/***************************************************
	REPLICATION CODE FOR:
		HARVESTING THE RAIN
		AKER AND JACK
		RESTAT
	
	SEE README FOR ADDITIONAL INFORMATION
***************************************************/

////////////
// SET UP // 
////////////

	clear
	set more off
	set mem 300m
	cap set matsize 5000
	cap set maxvar 3200
	version 14
	set scheme s1mono
	cap ssc install scheme_tufte
	*set scheme tufte
	graph set window fontface "Helvetica"

	// filepaths // 
	* cap cd " " // Add your filepath here
	
	global data = "Replication/data"
	global do = "Replication/code"
	global out = "Replication/output"
	
	// control vars // 
	
	global tstat= "female_l hhsize_l mobile_l hectares_l degraded_ha_l demilune_l"
	
	*graph set window fontface "Latin Modern Sans" 

////////////////////////////////
// PREPARE FILES FOR ANALYSIS // 
////////////////////////////////
	
	//BASELINE
	
	use "$data/input/baseline.dta", replace
	egen cropsgrown=rowtotal(mil_grown sorgho_grown niebe_grown arach_grown sesa_grown) if foundresp==1 & okaytosurvey==1
	la var cropsgrown "Total number of crops cultivated"
	egen totalprodkg=rowtotal(milqtykg sorghoqtykg cowpeaqtykg arachqtykg sesaqtykg) if attrition==0 & okaytosurvey==1 & cropsgrown!=.
	la var totalprodkg "Total quantity of crops produced (in kg)"
	gen slope_correct=(testdl_slope==1) if foundresp!=0
	gen crops_correct=(testdl_crops==0) if foundresp!=0
	gen sandy_correct=(testdl_sandy==0) if foundresp!=0
	gen qty_correct=(testdl_qty==0) if foundresp!=0
	gen space_correct=(testdl_spacing==0) if foundresp!=0
	gen size_correct=(testdl_size==0) & foundresp!=0
	gen timing_correct=(testdl_dimension==0) & foundresp!=0
	gen depth_correct=(testdl_depth==0) & foundresp!=0
	egen testscore=rowtotal(*_correct)
	replace testscore=. if foundresp==0
	save "$data/baseline.dta", replace
	clear
	
	//MIDLINE AND ENDLINE DATA
	
	use "$data/input/endline_midline_panel.dta", replace
	
	label var anytreat "Any treatment"
	
	egen totaldlexpense=rowtotal(dlnonfamlabor_expense dlfamlabor_expense) if foundresp==1
	la var totaldlexpense "Total amount spent on labor for demi-lunes, unconditional"

	gen dlnonfamlabor_expense0=dlnonfamlabor_expense
	replace dlnonfamlabor_expense0=0 if dlnonfamlabor_expense==. & foundresp==1
	la var dlnonfamlabor_expense0 "Expenditures on non-family labor for DL (unconditional)"

	gen dlfamlabor_expense0=dlfamlabor_expense
	replace dlfamlabor_expense0=0 if dlfamlabor_expense==. & foundresp==1
	la var dlfamlabor_expense0 "Expenditures on family labor for demi-lunes, unconditional"

	gen nondllabor_expense0=nondllabor_expense
	replace nondllabor_expense0=0 if nondllabor_expense==. & foundresp==1

	gen totalexpenseperdl=totaldlexpense/demilune_builtno

	egen totallaborexpense=rowtotal(dlnonfamlabor_expense nondllabor_expense) if foundresp==1
	la var totallaborexpense "Total amount spent on non-family labor, unconditional"

	gen famlabor_revenue0=famlabor_revenue
	replace famlabor_revenue0=0 if famlabor_revenue==. & foundresp==1 & okaytosurvey==1
	la var famlabor_revenue0 "Total revenue from selling family labor, unconditional"
	
	egen totalprodkg=rowtotal(milqtykg sorghoqtykg cowpeaqtykg arachqtykg sesaqtykg) if attrition==0 & okaytosurvey==1 & cropsgrown!=.
la var totalprodkg "Total quantity of crops produced (in kg)"

	gen milval=174*milqtykg
	gen sorval=179*sorghoqtykg
	gen cowval=250*cowpeaqtykg
	gen sesval=500*sesaqtykg
	gen araval=370*arachqtykg

	egen totalprodcfa=rowtotal(milval sorval cowval sesval araval) if attrition==0 & totalprodkg!=.

	egen hdds=rowtotal(cereal tubercule legumes vegetables fruits meat fish eggs milk oil condiments sugar)
	replace hdds=. if foundresp==0
	replace hdds=. if hdds==0

	gen dlfamlabor_persondays0=dlfamlabor_persondays
	replace dlfamlabor_persondays0=0 if dlfamlabor_persondays==. & attrition==0 & fields_owner!=.
	la var dlfamlabor_persondays0 "Number of person-days of family labor used for demi-lunes, unconditional"
	order dlfamlabor_persondays0, a(dlfamlabor_persondays)
		
	gen dlnonfamlabor_persondays0=dlnonfamlabor_persondays
	replace dlnonfamlabor_persondays0=0 if dlnonfamlabor_persondays==. & attrition==0 & fields_owner!=.
	la var dlnonfamlabor_persondays0 "Number of person-days of non-family labor hired for demi-lunes, unconditional "
		
	gen famlaborsold_nbr0=famlaborsold_nbr
	replace famlaborsold_nbr0=0 if famlaborsold_nbr==. & famlaborsold==0
	la var famlaborsold_nbr0 "Number of days of household labor sold, unconditional"

	save "$data/endline_midline_panel.dta", replace
	clear


	//DEMI-LUNES DATA

	use "$data/input/dlconstruction2018-2020.dta"

	gen dlperha=interviewer_demilunescounthq0/degraded_ha
	la var dlperha "Number of verified DL constructed per number of degraded hectares"

	gen dlqualityratio=interviewer_demilunescounthq/interviewer_demilunescount
	la var dlquality "Ratio of verified to total demi-lunes constructed"

	replace dlqualityratio=interviewer_demilunescounthq0/interviewer_demilunescount0 if time==3

	save "$data/dlconstruction2018-2020.dta", replace
	clear

	//SPILLOVERS DATA

	use "$data/input/endlinespillover.dta", replace

	egen num_dl_tot = rowtotal(demilune_builtno2018 demilune_builtno2019 demilune_builtno2020)
	replace num_dl_tot = . if attrition==1
	label var num_dl_tot "Total number of DL constructed over 3 years"

	gen slope_correct=(testdl_slope==0) if attrition==0
	gen crops_correct=(testdl_crops_mil==1 & testdlcrops_sorgho==1 & testdlcrops_niebe==1) if attrition==0
	gen sandy_correct=(testdl_sandy==0 & testdl_sandyclay==0 & testdl_glacis==1) if attrition==0
	gen qty_correct=(testdl_qty>=250 & testdl_qty<=300) & attrition==0
	gen space_correct=(testdl_spacing==2) if attrition==0
	gen size_correct=(testdl_size==4) if attrition==0
	gen depth_correct=(testdl_depth==1) if attrition==0
	gen input_correct=(testdl_noinput==1 & testdl_input1==0 & testdl_input2==0 & testdl_input4==0 & testdl_input5==0) & attrition==0
	gen compass_correct=(testdl_needcompass==1) & attrition==0

	egen testscore=rowtotal(*_correct)
	replace testscore=. if foundresp==0
	replace testscore=. if attrition==0  & testdl_slope==.

	save "$data/endlinespillover.dta", replace
	clear
	
	//OTHER -- NO PREP
	
	use "$data/input/dlconstruction2021.dta", replace
	save "$data/dlconstruction2021.dta", replace
	
	use "$data/input/cashtransfer.dta", replace
	save "$data/cashtransfer.dta", replace
	
	use "$data/input/training.dta", replace
	save "$data/training.dta", replace

/////////////////
// MAIN TABLES // 
/////////////////
	

**********************************
*TABLE 1: DEMI-LUNE ADOPTION 2018*
**********************************

	use "$data/dlconstruction2018-2020.dta", replace
	est clear

	global demilunes="anydemilune interviewer_demilunescounthq0 dlperha dlqualityratio interviewer_demilunessowed0 interviewer_intrants0"
	global tstat1 = "female_l hhsize_l mobile_l hectares_l demilune_l" // does not include ha of degraded land bc it is included in dlperha

	local k=1
		foreach i in $demilunes{
			
		// Panel A
		xi: reg `i' anytreat kantche vsize admin $tstat1 i.cluster if time==1, r cluster(villID)
		
		matrix A = r(table)[1..9,"anytreat"]
		
		// Panel B
		xi: reg `i' training uct_early cct uct_late kantche vsize admin $tstat1 i.cluster if time==1, r cluster(villID)
		
		matrix A = A, r(table)
		estadd matrix bnew = A["b",1.."_cons"]
		estadd matrix se = A["se",1.."_cons"]
		estadd matrix p = A["pvalue",1.."_cons"]

		sum `i' if anytreat==0 & time==1
		estadd sca ctlm = r(mean)

		test training=cct=uct_early=uct_late=0
		estadd sca pval=r(p)
		
		testparm training uct_early, equal
		qui estadd sca pval1=r(p)
		testparm training cct, equal
		qui estadd sca pval2=r(p)
		testparm training uct_late, equal
		qui estadd sca pval3=r(p)
		testparm uct_early cct, equal
		qui estadd sca pval4=r(p)
		testparm uct_early uct_late, equal
		qui estadd sca pval5=r(p)
		testparm cct uct_late, equal
		qui estadd sca pval6=r(p)
		
		eststo C`k'
		local k = `k'+1			
		}

	la var anytreat "Any treatment"
	la var training "Training"
	la var uct_early "UCT early"
	la var uct_late "UCT late"
	la var cct "CCT"
		
	esttab C1 C2 C3 C4 C5 C6 using "$out/table1.tex", booktabs ///
	replace noobs cells(bnew(fmt(%10.2f) star pvalue(p)) se(par)) wrap  star(* 0.10 ** 0.05 *** 0.01)  ///
	scalars("ctlm Mean in control" "N No. of observations" "r2 R squared" "pval1 \hline Training=UCT early" "pval2 Training=CCT" "pval3 Training=UCT late" "pval4 UCT early=CCT" "pval5 UCT early=UCT late" "pval6 CCT=UCT late") ///
	sfmt(%10.2f %10.0f %10.2f %10.2f %10.2f %10.2f %10.2f %10.2f %10.2f) ///
	mlabels("\specialcell{Constructed \\ any demi-lune}" "\specialcell{No. of \\ verified \\ demi-lunes \\ constructed}" "\specialcell{No. of \\ verified \\ demi-lunes \\ constructed \\ per degraded ha}" "\specialcell{Ratio of verified \\ to total no. \\ of demi-lunes \\ constructed}" "\specialcell{Sowed in \\ demi-lunes}" "\specialcell{Used \\ manure in \\ demi-lunes}") ///
	align(cccccccc) width(\hsize) label collabels(,none) numbers nonotes order(anytreat) ///
	keep(anytreat training uct_early cct uct_late) refcat(anytreat "\emph{\underline{Panel A}}" training "\hline\emph{\underline{Panel B}}", nolabel)



*********************
*TABLE 2: SPILLOVERS*
*********************

	use "$data/dlconstruction2018-2020.dta", replace
	est clear

	global neighbors="neighbors interviewer_dlneighbors"
	global tstat= "female_l hhsize_l mobile_l hectares_l degraded_ha_l demilune_l"

	local k=1
		foreach i in $neighbors{   
		xi: reg `i' anytreat kantche vsize admin $tstat i.cluster if time==3, r cluster(villID)
		
		sum `i' if anytreat==0 & time==3
		estadd sca ctlm_spill = r(mean)

		eststo spill`k'
		local k = `k'+1
		}

	la var anytreat "Any treatment"
	la var training "Training"
	la var uct_early "UCT early"
	la var uct_late "UCT late"
	la var cct "CCT"


	// Last three columns from endline spillover survey
	use "$data/endlinespillover.dta", replace

	global spillover="demilune_last3 num_dl_tot" 

	local k=3
		foreach i in $spillover{   
		xi: reg `i' anytreat kantche vsize admin $tstat i.cluster if time==3, r cluster(villID)
		
		sum `i' if anytreat==0 & time==3
		estadd sca ctlm_spill = r(mean)

		eststo spill`k'
		local k = `k'+1
		}

	la var anytreat "Any treatment"
	la var training "Training"
	la var uct_early "UCT early"
	la var uct_late "UCT late"
	la var cct "CCT"
		
	esttab spill* using "$out/table2.tex", booktabs ///
	replace label nonotes noobs wrap b(%10.2fc) se(%10.2fc) star(* 0.10 ** 0.05 *** 0.01) keep(anytreat) ///
	scalars("ctlm_spill Mean in control" "N No. of observations" "r2 R squared") ///
	sfmt(%10.2fc %10.0fc %10.2fc) ///
	mlabels("\specialcell{Any demi-lunes \\ adopted on \\neighbors' plots \\}" "\specialcell{No. of fields \\ where \\ neighbors' \\ adopted}" "\shortstack{Adopted any \\ demi-lunes}" "\shortstack{Total \\ no. of \\ demi-lunes}" ) 


****************
*TABLE 3: LABOR*
****************

	use "$data/endline_midline_panel.dta", replace
	est clear

	global labormidline="dlfamlabor_persondays0 dlnonfamlabor_persondays0 migrate_nbr famlaborsold_nbr0 nondllabor_used"
	global laborendline="migrate_nbr famlaborsold_nbr0 nondllabor_used"

	// Panel A
	local j=1 
	foreach i in $labormidline {   
		xi: reg `i' anytreat kantche vsize admin $tstat i.cluster if time==2, r cluster(villID)	
		eststo mid`j'
		summ `i' if anytreat==0 & e(sample) == 1
		estadd scalar conmean_mid = r(mean)
		estadd scalar consd_mid = r(sd)
		local j=`j'+1
	}

	esttab mid* using "$out/table3.tex", booktabs ///
	replace label nonotes noobs wrap b(%10.2fc) se(%10.2fc) star(* 0.10 ** 0.05 *** 0.01) keep(anytreat) ///
	refcat(anytreat "\emph{\underline{Panel A: Year 1}}", nolabel) ///
	scalars("conmean_mid Mean in control" "N No. of observations" "r2 R squared") ///
	sfmt(%10.2fc %10.0fc %10.2fc) ///
	mlabels("\shortstack{Person-days \\ of DL \\ family \\ labor used}" "\shortstack{Person-days \\ of DL \\ non-family \\ labor used}" "\shortstack{No. of \\ migrants}" "\shortstack{No. of \\ family \\ members \\ selling labor}" "\shortstack{Hired any \\ non-family \\ non-DL labor}" ) ///
	postfoot("")

	// Panel B
	local j=3 
	foreach i in $laborendline {   	
		xi: reg `i' anytreat kantche vsize admin $tstat i.cluster if time==3, r cluster(villID)
		eststo end`j'
		summ `i' if anytreat==0 & e(sample) == 1
		estadd scalar conmean_end = r(mean)
		estadd scalar consd_end = r(sd)
		local j=`j'+1
	}

	// Append panel B 
	esttab end* using "$out/table3.tex", booktabs extracols(1 1) ///
	append label nomtitles nonumbers nonotes noobs wrap b(%10.2fc) se(%10.2fc) star(* 0.10 ** 0.05 *** 0.01) keep(anytreat) ///
	refcat(anytreat "\emph{\underline{Panel B: Year 3}}", nolabel) ///
	scalars("conmean_end Mean in control" "N No. of observations" "r2 R squared") ///
	sfmt(%10.2fc %10.0fc %10.2fc) ///
	subst(\multicolumn{4} \multicolumn{7}) ///
	prehead("") 

		
********************************
*TABLE 4: AGRICULTURAL OUTCOMES*
********************************

	use "$data/endline_midline_panel.dta", replace
	est clear

	global productionmidline="cropattempt_nbr cropfailpct2" //  totalprodkg totalprodcfa
	global productionmidline_z = "totalprodkg totalprodcfa"
	global productionendline="cropattempt_nbr cropfailpct2" //  totalprodkg totalprodcfa 
	global productionendline_z="totalprodkg totalprodcfa"

	// Panel A
	local j=1 
	foreach i in $productionmidline {   
		xi: reg `i' anytreat kantche vsize admin $tstat i.cluster if time==2, r cluster(villID)	
		eststo mid`j'
		summ `i' if anytreat==0 & e(sample) == 1
		estadd scalar conmean_mid = r(mean)
		estadd scalar consd_mid = r(sd)
		local j=`j'+1
	}
	local j=3 
	foreach i in $productionmidline_z {   
		xi: reg z_`i' anytreat kantche vsize admin $tstat i.cluster if time==2, r cluster(villID)	
		eststo mid`j'
		summ `i' if anytreat==0 & e(sample) == 1
		estadd scalar conmean_mid = r(mean)
		estadd scalar consd_mid = r(sd)
		local j=`j'+1
	}
	esttab mid* using "$out/table4.tex", booktabs ///
	replace label nonotes noobs wrap b(%10.2fc) se(%10.2fc) star(* 0.10 ** 0.05 *** 0.01) keep(anytreat) ///
	refcat(anytreat "\emph{\underline{Panel A: Year 1}}", nolabel) ///
	scalars("conmean_mid Mean in control" "consd_mid SD in control" "N No. of observations" "r2 R squared") ///
	sfmt(%10.2fc %10.2fc %10.0fc %10.2fc) ///
	mlabels("\shortstack{No. of \\ crops \\ planted}" "\shortstack{Percentage \\ crops failed of \\ crops attempted}" "\shortstack{Z-score of \\ production (kg) \\ of crops }" "\shortstack{Z-score of \\ value (CFA) \\ of crop production}") ///
	postfoot("")

	// Panel B
	local j=1 
	foreach i in $productionendline {   	
		xi: reg `i' anytreat kantche vsize admin $tstat i.cluster if time==3, r cluster(villID)
		eststo end`j'
		summ `i' if anytreat==0 & e(sample) == 1
		estadd scalar conmean_end = r(mean)
		estadd scalar consd_end = r(sd)
		local j=`j'+1
	}
	local j=3 
	foreach i in $productionendline_z {   	
		xi: reg z_`i' anytreat kantche vsize admin $tstat i.cluster if time==3, r cluster(villID)
		eststo end`j'
		summ `i' if anytreat==0 & e(sample) == 1
		estadd scalar conmean_end = r(mean)
		estadd scalar consd_end = r(sd)
		local j=`j'+1
	}
	// Panel B 
	esttab end* using "$out/table4.tex", booktabs ///
	append label nomtitles nonumbers nonotes noobs wrap b(%10.2fc) se(%10.2fc) star(* 0.10 ** 0.05 *** 0.01) keep(anytreat) ///
	refcat(anytreat "\emph{\underline{Panel B: Year 3}}", nolabel) ///
	scalars("conmean_end Mean in control" "consd_end SD in control" "N No. of observations" "r2 R squared") ///
	sfmt(%10.2fc %10.2fc %10.0fc %10.2fc) ///
	subst(\multicolumn{4} \multicolumn{7}) ///
	prehead("") 


************************
*TABLE 5: LAND OUTCOMES*
************************

	use "$data/endline_midline_panel.dta", replace 
	est clear

	foreach x in soilqualityimprove vegetationreturned waterretention soilqualitydegrade  vegetationdisappeared {
		replace `x' = 0 if noticesoilchange == 0
	}

	global landmidline="fields_number cultivatelands2017 degradedcult_ha soil_mean"
	global landendline = "fields_number cultivatenewland nolongercultivate soilqualityimprove "

	// Panel A
	local j=1 
	foreach i in $landmidline {
		xi: reg `i' anytreat kantche vsize admin $tstat i.cluster if time==2, r cluster(villID)	
		eststo mid`j'
		summ `i' if anytreat==0 & e(sample) == 1
		estadd scalar conmean_mid = r(mean)
		estadd scalar consd_mid = r(sd)
		local j=`j'+1
	}

	// Setup table with panel A
	esttab mid* using "$out/table5.tex", booktabs ///
	replace label nonotes noobs noomit wrap b(%10.2fc) se(%10.2fc) star(* 0.10 ** 0.05 *** 0.01) keep(anytreat) ///
	subst(\begin{tabular}{l*{4}{c} \begin{tabular}{l*{4}{c}) ///
	refcat(anytreat "\emph{\underline{Panel A: Year 1}}", nolabel) ///
	scalars("conmean_mid Mean in control" "N No. of observations" "r2 R squared") ///
	sfmt(%10.2fc %10.0fc %10.2fc) ///
	mlabels("\shortstack{No. of \\ fields owned \\ or rented}" "\shortstack{Ha. of land \\ cultivated}" "\shortstack{Ha. of degraded \\ land cultivated}" "\shortstack{Self-reported \\ soil quality}") ///
	postfoot("")

	// Panel B
	local j=1 
	foreach i in $landendline {   	
		xi: reg `i' anytreat kantche vsize admin $tstat i.cluster if time==3, r cluster(villID)
		eststo end`j'
		summ `i' if anytreat==0 & e(sample) == 1
		estadd scalar conmean_end = r(mean)
		estadd scalar consd_end = r(sd)
		local j=`j'+1
	}

	// Append panel B 
	esttab end* using "$out/table5.tex", booktabs ///
	append label nonotes noobs wrap b(%10.2fc) se(%10.2fc) star(* 0.10 ** 0.05 *** 0.01) keep(anytreat) ///
	refcat(anytreat "\emph{\underline{Panel B: Year 3}}", nolabel) ///
	scalars("conmean_end Mean in control" "N No. of observations" "r2 R squared") ///
	sfmt(%10.2fc %10.0fc %10.2fc) ///
	mlabels("\shortstack{No. of \\ fields owned \\ or rented}" "\shortstack{Renewed \\ cultivation on \\ any land}" "\shortstack{Stopped \\ cultivating \\ any land}" "\shortstack{Soil \\ quality \\ improved}" ) ///
	prehead("\toprule") 
	
	
//////////////////
// MAIN FIGURES // 
//////////////////



****************************
*FIGURE 1: ADOPTION, YEAR 1*
****************************

	use "$data/dlconstruction2018-2020.dta", replace
	est clear
	
	*set scheme tufte

	xi: reg anydemilune training uct_early cct uct_late kantche vsize admin $tstat i.cluster if time==1, r cluster(villID)
	iegraph training uct_early cct uct_late, basicti("") ///
	gray ytitle("Any demi-lunes, Year 1") yscale(titlegap(5pt)) ///
	legend(off) xlabel(1 "Control" 2 "Training" 3 "UCT early" 4 "CCT" 5 "UCT late", ///
	labsize(medium) tstyle(major_notick)) ///
	graphregion(fcolor(white) lcolor(white)) xsize(6.5)
	
	graph export "$out/fig1a.png", replace
	
	xi: reg interviewer_demilunescounthq0  training uct_early cct uct_late kantche vsize admin $tstat i.cluster if time==1, r cluster(villID)
	iegraph training uct_early cct uct_late, basicti("") ///
	gray ytitle("No. of verified demi-lunes unconditional, Year 1") yscale(titlegap(5pt)) ///
	legend(off) xlabel(1 "Control" 2 "Training" 3 "UCT early" 4 "CCT" 5 "UCT late", ///
	labsize(medium) tstyle(major_notick)) ///
	graphregion(fcolor(white) lcolor(white)) yscale(range(0(20)50)) ylabel(0(20)50) xsize(7)
	
	graph export "$out/fig1b.png", replace

*****************************;
*FIGURE 2 ADOPTION OVER TIME*;
*****************************;

	use "$data/dlconstruction2018-2020.dta", replace
	est clear

	sort menagecode time
	
	xi: reg anydemilune anytreat kantche vsize admin $tstat i.cluster if time==1, r cluster(villID)
		iegraph anytreat, basicti("") title("2018", size(medsmall) color(black)) ///
		gray ytitle("Any demi-lunes, Year 1") ymtick(##4) yscale(titlegap(5pt)) ///
		legend(row(1) order(1 "Control" 2 "Any treat") size(small) ///
		symxsize(4pt) colgap(1pt) keygap(1pt)) ///
		graphregion(fcolor(white)) xsize(6.5) baroptions(barwidth(0.75))
		graph rename p1, replace
		
		xi: reg anydemilune anytreat kantche vsize admin $tstat i.cluster if time==2, r cluster(villID)
		iegraph anytreat, basicti("") title("2019", size(medsmall) color(black)) ///
		gray ytitle("Any demi-lunes, Year 2") ymtick(##4) yscale(titlegap(5pt)) ///
		legend(row(1) order(1 "Control" 2 "Any treat") size(small) ///
		symxsize(4pt) colgap(1pt) keygap(1pt)) ///
		graphregion(fcolor(white)) xsize(6.5) baroptions(barwidth(0.75))
		graph rename p2, replace
		
		xi: reg anydemilune anytreat kantche vsize admin $tstat i.cluster if time==3, r cluster(villID)
		iegraph anytreat, basicti("") title("2020", size(medsmall) color(black)) ///
		gray ytitle("Any demi-lunes, Year 3") ymtick(##4) yscale(titlegap(5pt)) ///
		legend(row(1) order(1 "Control" 2 "Any treat") size(small) ///
		symxsize(4pt) colgap(1pt) keygap(1pt)) ///
		graphregion(fcolor(white)) xsize(6.5) baroptions(barwidth(0.75))
		graph rename p3, replace
		
		graph combine p1 p2 p3, rows(1) ycommon graphregion(color(white)) iscale(0.75)
	
	graph export "$out/fig2a.png", replace
	
	xi: reg interviewer_demilunescounthq0 anytreat kantche vsize admin $tstat i.cluster if time==1, r cluster(villID)
		iegraph anytreat, basicti("") title("2018", size(medsmall) color(black)) ///
		gray ytitle("No of verified demi-lunes unconditional, Year 1") ymtick(##4) yscale(titlegap(5pt)) ///
		legend(row(1) order(1 "Control" 2 "Any treat") size(small) ///
		symxsize(4pt) colgap(1pt) keygap(1pt)) ///
		graphregion(fcolor(white)) xsize(6.5) baroptions(barwidth(0.75))
		graph rename p1, replace
		
		xi: reg interviewer_demilunescounthq0 anytreat kantche vsize admin $tstat i.cluster if time==2, r cluster(villID)
		iegraph anytreat, basicti("") title("2019", size(medsmall) color(black)) ///
		gray ytitle("No of verified demi-lunes unconditional, Year 2") ymtick(##4) yscale(titlegap(5pt)) ///
		legend(row(1) order(1 "Control" 2 "Any treat") size(small) ///
		symxsize(4pt) colgap(1pt) keygap(1pt)) ///
		graphregion(fcolor(white)) xsize(6.5) baroptions(barwidth(0.75))
		graph rename p2, replace
		
		xi: reg interviewer_demilunescounthq0 anytreat kantche vsize admin $tstat i.cluster if time==3, r cluster(villID)
		iegraph anytreat, basicti("") title("2020", size(medsmall) color(black)) ///
		gray ytitle("No of verified demi-lunes unconditional, Year 3") ymtick(##4) yscale(titlegap(5pt)) ///
		legend(row(1) order(1 "Control" 2 "Any treat") size(small) ///
		symxsize(4pt) colgap(1pt) keygap(1pt)) ///
		graphregion(fcolor(white)) xsize(6.5) baroptions(barwidth(0.75))
		graph rename p3, replace
		
		graph combine p1 p2 p3, rows(1) ycommon graphregion(color(white)) iscale(0.75)

	graph export "$out/fig2b.png", replace

***********************:
*FIGURE 3 TEST SCORES*;
***********************;

	use "$data/endline_midline_panel.dta", replace
	gen spillover = 0
	append using "$data/endlinespillover.dta", force
	replace spillover = 1 if spillover == .
	est clear

	global endlinetest="slope_correct crops_correct sandy_correct qty_correct space_correct size_correct depth_correct "

	foreach i in $endlinetest{
		gen `i'_treat=anytreat 
	}
		
	foreach i in $endlinetest{
		
		xi: reg `i' `i'_treat kantche vsize admin i.cluster if spillover==0, r cluster(villagecode)
		estimates store `i'
		gen `i'_s = `i' if spillover == 1
		xi: reg `i'_s `i'_treat kantche vsize admin i.cluster if spillover==1, r cluster(villagecode)
		estimates store `i'_s
	}

	la var slope_correct_treat "Knows must be on a slope"
	la var crops_correct_treat "Knows can plant crops in demi-lunes"
	la var sandy_correct_treat "Knows cannot be built on sandy soil"
	la var qty_correct_treat  "Knows should construct 250 demi-lunes/ha"
	la var space_correct_treat "Knows correct spacing for demi-lunes"
	la var size_correct_treat "Knows correct dimensions of demi-lunes"
	la var depth_correct_treat "Knows correct depth of demi-lunes"	
		
				coefplot	(slope_correct crops_correct sandy_correct qty_correct space_correct size_correct depth_correct , label(Main sample)) (slope_correct_s crops_correct_s sandy_correct_s qty_correct_s space_correct_s size_correct_s depth_correct_s , label(Spillover sample)), keep (*_correct_treat) xline(0) xtitle(Any Treatment, size(medsmall)) legend(size(small)) graphregion(margin(l=75)) yscale(alt noline) coeflabels(, labsize(medsmall) labgap(-125) notick ) mlcolor(black) graphregion(fcolor(white)) 

		graph export "$out/fig3.png", replace
		
	
/////////////////////
// APPENDIX TABLES // 
/////////////////////


*********************************
*TABLE A1: RCT EFFECT SIZE TABLE*
*********************************

	* See text
	

***********************
*TABLE A2: DATA SUMMARY*
***********************

	* See text

**************************
*TABLE A3: LISTING BALANCE*
**************************

	use "$data/baseline.dta", replace
	est clear
	
	global listing="female_l hhsize_l mobile_l hectares_l degraded_ha_l demilune_l"
	global baseline="respondentage hausa hhreadandwrite HHsize_b asset_index fields_number cropsgrown totalprodkg migrate_nbr season_nofood adultnoeat demilune_lastharvest0 demilune_builtno0 testscore"
	
	loc nvar=6
	
	foreach k in C1 C2 C3 C4 C5 P N S1 S2 S3 S4 S5 P1 P2 P3 P4 P5{  
	mat `k'=J(1,`nvar',1)
	mat colnames `k' = $listing
	}

	loc k=1
	foreach var in $listing { 
	xi: reg `var' training uct_early cct uct_late kantche vsize admin i.cluster, r cluster(villID)
	test training uct_early cct uct_late  
	mat N[1,`k']= e(N)

	mat P[1,`k']=r(p)
	sum `var' if treat==1 & e(sample)==1

	mat C1[1,`k']=r(mean)
	mat S1[1,`k']=r(sd)

	mat C2[1,`k']=_b[training]
	mat S2[1,`k']= _se[training]
	test training
	mat P2[1,`k']=r(p)


	mat C3[1,`k']=_b[uct_early]
	mat S3[1,`k']= _se[uct_early]
	test uct_early
	mat P3[1,`k']=r(p)

	mat C4[1,`k']=_b[cct]
	mat S4[1,`k']= _se[cct]
	test cct
	mat P4[1,`k']=r(p)

	mat C5[1,`k']=_b[uct_late]
	mat S5[1,`k']= _se[uct_late]
	test uct_late
	mat P5[1,`k']=r(p)

	loc k = `k'+1 /*add to counter*/
	}

	foreach j in C1 C2 C3 C4 C5 P N{
	estpost summarize $listing
	matrix rename `j' new, replace
	estadd matrix new, replace 

	matrix obs= new
	estadd matrix obs, replace
	estimates store col_`j'
	}

	foreach k in 1 2 3 4 5 { 
	estimates restore col_C`k' 
	matrix rename S`k' sdev, replace 
	estadd matrix sdev, replace 
	matrix rename P`k' pval, replace
	estadd matrix pval, replace
	estimates store col_C`k'
	}

	* shorten variable names*
	desc $listing
	la var female_l "Respondent is female" 
	la var hhsize_l "No. of adult household members" 
	la var mobile_l "Owns a mobile phone"
	la var hectares_l "No. of hectares owned"
	la var degraded_ha_l "No. of degraded hectares owned"
	la var demilune_l "Demi-lune experience"
 
	esttab col_C1 col_C2 col_C3 col_C4 col_C5 col_P col_N using "$out/tablea3.tex", ///
	cells("new(fmt(2) star pvalue(pval) pattern(1 1 1 1 1 1 0))&obs(fmt(0) pattern(0 0 0 0 0 0 1))" sdev(par)) ///
	notes replace label star(* 0.10 ** 0.05 *** 0.01) booktabs ///
	mlabels("Control" "Training" "\specialcell{UCT \\early}" "CCT" "\specialcell{UCT \\late}" "\specialcell{P-value of \\ joint F test}" "N") ///
	noobs collabels(,none) alignment(ccccc) gaps width(\hsize) wrap 
	

*****************************;
*TABLE A4: BASELINE BALANCE*;
*****************************;

	use "$data/baseline.dta", replace
	est clear

	loc nvar=16
	local varlist2="respondentage hausa hhreadandwrite HHsize_b asset_index mechanisms_groupsavings money_loaned money_borrow fields_number cropsgrown totalprodkg season_nofood adultnoeat migrate_nbr demilune_lastharvest0 testscore"

	foreach k in C1 C2 C3 C4 C5 P N S1 S2 S3 S4 S5 P1 P2 P3 P4 P5{  
	mat `k'=J(1,`nvar',1)
	mat colnames `k' = `varlist2'
	}

	loc k=1
	foreach var in `varlist2'{ 
	xi: reg `var' training uct_early cct uct_late kantche vsize admin i.cluster if baseline==1, r cluster(villID)
	test training uct_early cct uct_late  
	mat N[1,`k']= e(N)

	mat P[1,`k']=r(p)
	sum `var' if treat==1 & e(sample)==1

	mat C1[1,`k']=r(mean)
	mat S1[1,`k']=r(sd)

	mat C2[1,`k']=_b[training]
	mat S2[1,`k']= _se[training]
	test training
	mat P2[1,`k']=r(p)


	mat C3[1,`k']=_b[uct_early]
	mat S3[1,`k']= _se[uct_early]
	test uct_early
	mat P3[1,`k']=r(p)


	mat C4[1,`k']=_b[cct]
	mat S4[1,`k']= _se[cct]
	test cct
	mat P4[1,`k']=r(p)

	mat C5[1,`k']=_b[uct_late]
	mat S5[1,`k']= _se[uct_late]
	test uct_late
	mat P5[1,`k']=r(p)

	loc k = `k'+1 
	}
	
	foreach j in C1 C2 C3 C4 C5 P N{
	estpost summarize respondentage hausa hhreadandwrite HHsize_b asset_index fields_number cropsgrown totalprodkg migrate_nbr demilune_lastharvest0 testscore season_nofood adultnoeat  
	matrix rename `j' new, replace
	estadd matrix new, replace 


	matrix obs= new
	estadd matrix obs, replace
	estimates store col_`j'
	}

	foreach k in 1 2 3 4 5 { 
	estimates restore col_C`k' 
	matrix rename S`k' sdev, replace 
	estadd matrix sdev, replace 
	matrix rename P`k' pval, replace
	estadd matrix pval, replace
	estimates store col_C`k'
	}

	desc `varlist2'
	la var respondentage "Respondent age" 
	la var hausa "Respondent is Hausa" 
	la var hhreadandwrite "Received some schooling"
	la var HHsize_b "Household size" 
	la var asset_index "Asset index" 
	la var mechanisms_groupsavings "Household belonged to a savings group"
	la var money_loaned "Household lent food or money to someone in the past year"
	la var money_borrow "Household borrowed food or money from someone in the past year"
	la var fields_number "No. of fields owned or rented" 
	la var cropsgrown "No. of crops cultivated"
	la var totalprodkg "Total quantity produced"
	la var migrate_nbr "No. of household members who have migrated"
	la var demilune_lastharvest0 "Built demi-lunes prior season"
	la var demilune_builtno0 "No. of demi-lunes built"
	la var testscore "Demi-lune test score" 
	la var season_nofood "Household experienced hunger" 
	la var adultnoeat "Adult did not eat for an entire day"

	esttab col_C1 col_C2 col_C3 col_C4 col_C5 col_P col_N using "$out/tablea4.tex",cells("new(fmt(2) star pvalue(pval) pattern(1 1 1 1 1 1 0))&obs(fmt(0) pattern(0 0 0 0 0 0 1))" sdev(par)) notes replace label star(* 0.10 ** 0.05 *** 0.01) booktabs ///
	mlabels("Control" "Training" "\specialcell{UCT \\early}" "CCT" "\specialcell{UCT \\late}" "\specialcell{P-value of \\ joint F test}" "N") ///
	noobs collabels(,none) gaps nowrap 

****************************************
*TABLE A5: LISTING BALANCE BY TREATMENT*
****************************************

	use "$data/baseline.dta", replace
	est clear

	global listing="female_l hhsize_l mobile_l hectares_l degraded_ha_l demilune_l"

	la var female_l "Respondent is female" 
	la var hhsize_l "No. of adult household members" 
	la var mobile_l "Owns a mobile phone"
	la var hectares_l "No. of hectares owned"
	la var degraded_ha_l "No. of degraded hectares owned"
	la var demilune_l "Demi-lune experience"

	foreach var in $listing{
		xi: reg `var' training uct_early cct uct_late kantche vsize admin i.cluster, r cluster(villID)
	testparm training uct_early, equal
			local pval1`var': di %10.2f r(p)
			testparm training cct, equal
			local pval2`var' : di %10.2f r(p)
			testparm training uct_late, equal
			local pval3`var' : di %10.2f r(p)
			testparm uct_early cct, equal
			local pval4`var': di %10.2f r(p)
			testparm uct_early uct_late, equal
			local pval5`var': di %10.2f r(p)
			testparm cct uct_late, equal
			local pval6`var': di %10.2f r(p)
	loc name_`var' : var label `var'
		}

	texdoc init "$out/tablea5.tex" , replace force 

	tex \maxsizebox*{\hsize}{\textheight}{
	tex \begin{tabular}{lSSSSSS}
	tex \toprule
	tex &  \multicolumn{1}{c}{Training} & \multicolumn{1}{c}{Training} & \multicolumn{1}{c}{Training} & \multicolumn{1}{c}{UCT early} & \multicolumn{1}{c}{UCT early} & \multicolumn{1}{c}{CCT}  \\
	tex &  \multicolumn{1}{c}{=UCT early} & \multicolumn{1}{c}{=CCT} & \multicolumn{1}{c}{=UCT late} & \multicolumn{1}{c}{=CCT} & \multicolumn{1}{c}{=UCT late} & \multicolumn{1}{c}{=UCT late}  \\
	tex &   \multicolumn{1}{c}{(1)} & \multicolumn{1}{c}{(2)} & \multicolumn{1}{c}{(3)}  & \multicolumn{1}{c}{(4)} & \multicolumn{1}{c}{(5)} & \multicolumn{1}{c}{(6)} \\
	tex \midrule
	tex & & \\
	//tex {\it Agriculture} \\\cmidrule(lr){1-1}
	foreach var in $listing { 
	tex `name_`var'' &  `pval1`var'' & `pval2`var'' & `pval3`var'' & `pval4`var'' & `pval5`var'' & `pval6`var''  \\
	}
	tex \bottomrule
	tex \end{tabular}
	tex }
	tex  \\ 

	texdoc close



*****************************************
*TABLE A6 BASELINE BALANCE BY TREATMENT;
*****************************************

	use "$data/baseline.dta", replace
	est clear

	loc nvar=13
	loc varlist2 "respondentage hausa hhreadandwrite HHsize_b asset_index fields_number cropsgrown totalprodkg migrate_nbr season_nofood adultnoeat  demilune_lastharvest0 testscore "

	foreach k in P1 P2 P3 P4 P5 P6{  
	mat `k'=J(1,`nvar',0)
	mat colnames `k' = `varlist2'
	}

	loc k=1
	foreach var in `varlist2'{ 
	xi: reg `var' training uct_early cct uct_late kantche vsize admin, r cluster(villID)

		
	testparm training uct_early, equal
	mat P1[1,`k']=r(p)

	testparm training cct, equal
	mat P2[1,`k']=r(p)

	testparm training uct_late, equal
	mat P3[1,`k']=r(p)

	testparm uct_early cct, equal
	mat P4[1,`k']=r(p)

	testparm uct_early uct_late, equal
	mat P5[1,`k']=r(p)

	testparm cct uct_late, equal
	mat P6[1,`k']=r(p)

		
		local k = `k'+1
		
		}

	foreach j in P1 P2 P3 P4 P5 P6 {
	estpost summarize  respondentage hausa hhreadandwrite HHsize_b asset_index fields_number cropsgrown totalprodkg migrate_nbr season_nofood adultnoeat demilune_lastharvest0 testscore    
	matrix rename `j' new, replace
	estadd matrix new, replace 

	estimates store col_`j'
	}

	desc `varlist2'
	la var respondentage "Respondent age" 
	la var hausa "Respondent is Hausa" 
	la var hhreadandwrite "Received some schooling"
	la var HHsize_b "Household size at baseline" 
	la var asset_index "Asset index" 
	la var fields_number "No. of fields owned or rented" 
	la var cropsgrown "No. of crops cultivated"
	la var sorghoqtykg "Kg of sorghum produced"
	la var demilune_lastharvest0 "Built demi-lunes prior season"
	la var demilune_builtno0 "Number of demi-lunes built"
	la var testscore "Demi-lune test score" 
	la var season_nofood "Household experienced hunger" 
	la var adultnoeat "Adult did not eat for an entire day"
	la var migrate_nbr "No. of household members who have migrated"
	la var totalprodkg "Total quantity produced"

	esttab col_P1 col_P2 col_P3 col_P4 col_P5 col_P6 using "$out/tablea6.tex", replace cells("new(fmt(2))") booktabs ///
	mlabels("\specialcell{Training \\=UCT early}" "\specialcell{Training \\ =CCT}"  "\specialcell{Training \\ =UCT late}" "\specialcell{UCT early \\ =CCT}" "\specialcell{UCT early \\ =UCT late}" "\specialcell{CCT \\ =UCT late}") ///
	varlabels(respondentage "Respondent age" hausa "Respondent is Hausa" hhreadandwrite "Received some schooling" HHsize_b "Household size at baseline" asset_index "Asset index" fields_number "No. of fields owned or rented" cropsgrown "No. of crops cultivated" totalprodkg "Total quantity of output"season_nofood "Household experienced hunger" adultnoeat "Adult did not eat for an entire day" migrate_nbr "No. of household members who have migrated" demilune_lastharvest0 "Built demi-lunes prior season" testscore "Demi-lune test score" ) /// 
	noobs collabels(,none)  alignment(cccccc) gaps width(\hsize) 



********************
*TABLE A7: ATTRITION
********************

	use "$data/baseline.dta", replace
	est clear

	keep if baseline==1

	xi: reg attrition anytreat kantche vsize admin $tstat i.cluster, robust cluster(villID)
	eststo attritA1
			
	xi: reg attrition training uct_early cct uct_late kantche vsize admin $tstat i.cluster, robust cluster(villID)
	eststo attritB1

	sum attrition  if control==1 & e(sample)==1
	estadd sca ym = r(mean)

	test training= uct_early
	estadd sca p1 = r(p)
	test training =cct
	estadd sca p2 = r(p)
	test training= uct_late
	estadd sca p3 = r(p)
	test uct_early =cct
	estadd sca p4 = r(p)
	test uct_early =uct_late
	estadd sca p5 = r(p)
	test cct= uct_late
	estadd sca p6 = r(p)


	use "$data/endline_midline_panel.dta", replace

	xi: reg attrition anytreat kantche vsize admin $tstat i.cluster if time==2, robust cluster(villID)
	eststo attritA2

	xi: reg attrition training uct_early cct uct_late kantche vsize admin $tstat i.cluster if time==2, robust cluster(villID)
	eststo attritB2

	sum attrition  if control==1 & e(sample)==1
	estadd sca ym = r(mean)

	test training= uct_early
	estadd sca p1 = r(p)
	test training =cct
	estadd sca p2 = r(p)
	test training= uct_late
	estadd sca p3 = r(p)
	test uct_early =cct
	estadd sca p4 = r(p)
	test uct_early =uct_late
	estadd sca p5 = r(p)
	test cct= uct_late
	estadd sca p6 = r(p)


	use "$data/dlconstruction2018-2020.dta", replace

	xi: reg attrition anytreat kantche vsize admin $tstat i.cluster if time==1, robust cluster(villID)
	eststo attritA3

	xi: reg attrition training uct_early cct uct_late kantche vsize admin $tstat i.cluster if time==1, robust cluster(villID)
	eststo attritB3

	sum attrition  if control==1 & e(sample)==1
	estadd sca ym = r(mean)

	test training= uct_early
	estadd sca p1 = r(p)
	test training =cct
	estadd sca p2 = r(p)
	test training= uct_late
	estadd sca p3 = r(p)
	test uct_early =cct
	estadd sca p4 = r(p)
	test uct_early =uct_late
	estadd sca p5 = r(p)
	test cct= uct_late
	estadd sca p6 = r(p)


	xi: reg attrition anytreat kantche vsize admin $tstat i.cluster if time==2, robust cluster(villID)
	eststo attritA4

	xi: reg attrition training uct_early cct uct_late kantche vsize admin $tstat i.cluster if time==2, robust cluster(villID)
	eststo attritB4

	sum attrition  if control==1 & e(sample)==1
	estadd sca ym = r(mean)

	test training= uct_early
	estadd sca p1 = r(p)
	test training =cct
	estadd sca p2 = r(p)
	test training= uct_late
	estadd sca p3 = r(p)
	test uct_early =cct
	estadd sca p4 = r(p)
	test uct_early =uct_late
	estadd sca p5 = r(p)
	test cct= uct_late
	estadd sca p6 = r(p)

	xi: reg attrition anytreat kantche vsize admin $tstat i.cluster if time==3, robust cluster(villID)
	eststo attritA5

	xi: reg attrition training uct_early cct uct_late kantche vsize admin $tstat i.cluster if time==3, robust cluster(villID)
	eststo attritB5

	sum attrition  if control==1 & e(sample)==1
	estadd sca ym = r(mean)

	test training= uct_early
	estadd sca p1 = r(p)
	test training =cct
	estadd sca p2 = r(p)
	test training= uct_late
	estadd sca p3 = r(p)
	test uct_early =cct
	estadd sca p4 = r(p)
	test uct_early =uct_late
	estadd sca p5 = r(p)
	test cct= uct_late
	estadd sca p6 = r(p)


	use "$data/endline_midline_panel.dta", replace

	xi: reg attrition anytreat kantche vsize admin $tstat i.cluster if time==3, robust cluster(villID)
	eststo attritA6

	xi: reg attrition training uct_early cct uct_late kantche vsize admin $tstat i.cluster if time==3, robust cluster(villID)
	eststo attritB6

	sum attrition  if control==1 & e(sample)==1
	estadd sca ym = r(mean)

	test training= uct_early
	estadd sca p1 = r(p)
	test training =cct
	estadd sca p2 = r(p)
	test training= uct_late
	estadd sca p3 = r(p)
	test uct_early =cct
	estadd sca p4 = r(p)
	test uct_early =uct_late
	estadd sca p5 = r(p)
	test cct= uct_late
	estadd sca p6 = r(p)


	la var anytreat "Any treatment"
	la var training "Training"
	la var uct_early "UCT early"
	la var cct "CCT"
	la var uct_late "UCT late"


	// Panel A
	esttab attritA* using "$out/tablea7.tex", booktabs ///
	replace label nonotes noobs b(%10.2fc) se(%10.2fc) star(* 0.10 ** 0.05 *** 0.01) keep(anytreat) ///
	refcat(anytreat "\emph{\underline{Panel A}}", nolabel) ///
	mlabels("Baseline" "Midline" "DL 2018" "DL 2019" "DL 2020" "Endline") ///
	postfoot("")

	// Panel B
	esttab attritB* using "$out/tablea7.tex", booktabs ///
	append label nomtitles nonumbers nonotes noobs b(%10.2fc) se(%10.2fc) star(* 0.10 ** 0.05 *** 0.01) keep(training uct_early cct uct_late) ///
	refcat(training "\emph{\underline{Panel B}}", nolabel) ///
	scalars("p1 Training=UCT early" "p2 Training=CCT" "p3 Training=UCT late" "p4 UCT early=CCT" "p5 UCT early=UCT late" "p6 CCT=UCT late" ///
	"ym \midrule Mean in control" "N No. of observations" "r2 R squared" ) ///
	sfmt(%10.2fc %10.2fc %10.2fc %10.2fc %10.2fc %10.2fc %10.2fc %10.0fc %10.2fc) ///
	prehead("") 




**********************;
*TABLE A8: COMPLIANCE*;
**********************;

	use "$data/training.dta", replace
	est clear

	global attcash = "anytrain attended0 other_member0 trained_count"

	local k=1
		foreach i in $attcash{   
		xi: reg `i' anytreat kantche vsize admin $tstat i.cluster, r cluster(villID)
		//Store anytreat stats in matrix
		matrix A = r(table)[1..9,"anytreat" "female_l"]
		qui xi: reg `i' training uct_early cct uct_late kantche vsize admin $tstat i.cluster, r cluster(villID)
		matrix A = A, r(table)
		estadd matrix bnew = A["b",1.."_cons"]
		estadd matrix se = A["se",1.."_cons"]
		estadd matrix p = A["pvalue",1.."_cons"]
		
		
		sum `i' if control==1
		estadd sca ctlm = r(mean)
		
		testparm training uct_early, equal
		qui estadd sca pval1=r(p)
		testparm training cct, equal
		qui estadd sca pval2=r(p)
		testparm training uct_late, equal
		qui estadd sca pval3=r(p)
		testparm uct_early cct, equal
		qui estadd sca pval4=r(p)
		testparm uct_early uct_late, equal
		qui estadd sca pval5=r(p)
		testparm cct uct_late, equal
		qui estadd sca pval6=r(p)
		
		eststo C`k'
		local k = `k'+1
		}

	use "$data/cashtransfer.dta", replace

	gen anycash=(uct_early==1|uct_late==1|cct==1)
		
		
	local k=5
		foreach i in transferreceived0 amountreceived0{
		xi: reg `i' anycash kantche vsize admin $tstat i.cluster, r cluster(villID)
		//Store anycash stats in matrix
		matrix B = r(table)[1..9, "anycash" "female_l"]
		
		qui xi: reg `i' training uct_early cct uct_late kantche vsize admin $tstat i.cluster, r cluster(villID)
		matrix B = B, r(table)
		estadd matrix bnew = B["b",1.."_cons"]
		estadd matrix se = B["se",1.."_cons"]
		estadd matrix p =B["pvalue",1.."_cons"]
		
		sum `i' if control==1
		estadd sca ctlm = r(mean)
		
		testparm training uct_early, equal
		qui estadd sca pval1=r(p)
		testparm training cct, equal
		qui estadd sca pval2=r(p)
		testparm training uct_late, equal
		qui estadd sca pval3=r(p)
		testparm uct_early cct, equal
		qui estadd sca pval4=r(p)
		testparm uct_early uct_late, equal
		qui estadd sca pval5=r(p)
		testparm cct uct_late, equal
		qui estadd sca pval6=r(p)
		
		eststo C`k'
		local k = `k'+1
		}
		
	la var anycash "Any cash transfer"
	la var anytreat "Any treatment"
	la var training "Training"
	la var uct_early "UCT early"
	la var uct_late "UCT late"
	la var cct "CCT"
	la var female_l "Female"
		
	esttab C1 C2 C3 C4 C5 C6 using "$out/tablea8.tex", booktabs ///
	replace noobs cells(bnew(fmt(%10.2fc) star pvalue(p)) se(par)) wrap  star(* 0.10 ** 0.05 *** 0.01)  ///
	scalars("ctlm Mean in control" "N No. of observations" "r2 R squared" "pval1 \hline  Training=UCT early" "pval2 Training=CCT" "pval3 Training=UCT late" "pval4 UCT early=CCT" "pval5 UCT early=UCT Late" "pval6 CCT=UCT late") sfmt(%10.2f %10.0f %10.2f %10.2f %10.2f %10.2f %10.2f %10.2f %10.2f) ///
	mlabels("\specialcell{Any HH member \\ attended \\ training}" "\specialcell{Respondent \\ attended training}" "\specialcell{Another HH \\ member attended \\ training}" "\specialcell{No. of \\ respondents who \\ attended training }" "\specialcell{Cash transfer \\ recieved}" "\specialcell{Amount of \\ cash transfer \\ recieved in CFA}") ///
	align(cccccccc) width(\hsize) label collabels(,none) numbers nonotes order(anytreat anycash training uct_early cct uct_late female_l) keep(anytreat anycash training uct_early cct uct_late female_l) refcat(anytreat "\emph{\underline{Panel A}}" training "\hline\emph{\underline{Panel B}}", nolabel)



**********************************;
*TABLE A9: CORRELATES OF ADOPTION*;
**********************************;

	use "$data/dlconstruction2018-2020.dta", clear
	est clear


	global adoption= "anydemilune interviewer_demilunescounthq0"

	local k=1
		foreach i in $adoption{   
		xi: reg `i' anytreat kantche vsize admin female_l mobile_l hhsize_l hectares_l demilune_l z_totalprodcfa_b cropfailpct2_b testscore_b i.cluster if time==1, r cluster(villID)
		sum `i' if anytreat==0
		estadd sca ctlm = r(mean)
		

		
		eststo C`k'
		local k = `k'+1
		}

		
	la var anytreat "Any treatment"
	la var kantche "Kantche cluster"
	la var female_l "Female"
	la var mobile_l "Owns mobile phone"
	la var hhsize_l "No. of adults in HH"
	la var hectares_l "Total no. of hectares owned"
	la var demilune_l "Had previous experience with demi-lunes"
	la var z_totalprodcfa_b "Z-score of total value of production"
	la var cropfailpct2_b "Percentage of crops failed in terms of crops attempted"
	la var testscore_b "Demi-lune test"


	esttab C1 C2 using "$out/tablea9.tex", booktabs ///
	replace noobs cells(b(fmt(%10.2f) star) se(par)) star(* 0.10 ** 0.05 *** 0.01)  ///
	scalars("ctlm Mean in control" "N No. of observations" "r2 R squared") sfmt(%10.2f %10.0f %10.2f) ///
	mlabels("\specialcell{Constructed \\ any demi-lune}" "\specialcell{No. of verified demi-lunes \\ constructed}") ///
	align(cccccccc) width(\hsize) label collabels(,none) numbers nonotes order(anytreat) keep(anytreat kantche female_l mobile_l hhsize_l hectares_l demilune_l z_totalprodcfa_b cropfailpct2_b testscore_b ) 

	

**********************************;
*TABLE A10: DEMI-LUNE ADOPTION 2020*;
**********************************;

	use "$data/dlconstruction2018-2020.dta", replace
	est clear

	global demilunes="anydemilune interviewer_demilunescounthq0 dlperha dlqualityratio interviewer_dloperational0"
	global tstat1= "female_l hhsize_l mobile_l hectares_l demilune_l" // omit ha degraded land bc it is in denominator of col 3

	local k=1
		foreach i in $demilunes{
		xi: reg `i' anytreat kantche vsize admin $tstat1 i.cluster if time==3, r cluster(villID)

		matrix A1 = r(table)
		matrix A = A1[1..9,"anytreat"] 
		
		xi: reg `i' training uct_early cct uct_late kantche vsize admin $tstat1 i.cluster if time==3, r cluster(villID)

		matrix A = A, r(table)
		estadd matrix bnew = A["b",1.."_cons"]
		estadd matrix se = A["se",1.."_cons"]
		estadd matrix p = A["pvalue",1.."_cons"]

		sum `i' if anytreat==0 & time==3
		estadd sca ctlm = r(mean)

		test training=cct=uct_early=uct_late=0
		estadd sca pval=r(p)
		
		testparm training uct_early, equal
		qui estadd sca pval1=r(p)
		testparm training cct, equal
		qui estadd sca pval2=r(p)
		testparm training uct_late, equal
		qui estadd sca pval3=r(p)
		testparm uct_early cct, equal
		qui estadd sca pval4=r(p)
		testparm uct_early uct_late, equal
		qui estadd sca pval5=r(p)
		testparm cct uct_late, equal
		qui estadd sca pval6=r(p)
		
		eststo C`k'
		local k = `k'+1			
		}

	la var anytreat "Any treatment"
	la var training "Training"
	la var uct_early "UCT early"
	la var uct_late "UCT late"
	la var cct "CCT"
		
	esttab C1 C2 C3 C4 C5 using "$out/tablea10.tex", booktabs ///
	replace noobs cells(bnew(fmt(%10.2f) star pvalue(p)) se(par)) wrap  star(* 0.10 ** 0.05 *** 0.01)  ///
	scalars("ctlm Mean in control" "N No. of observations" "r2 R squared" "pval1 \hline Training=UCT Early" "pval2 Training=CCT" "pval3 Training=UCT late" "pval4 UCT Early=CCT" "pval5 UCT Early=UCT late" "pval6 CCT=UCT late") sfmt(%10.2f %10.0f %10.2f %10.2f %10.2f %10.2f %10.2f %10.2f %10.2f) ///
	mlabels("\specialcell{Constructed \\ any demi-lunes}" "\specialcell{No. of \\ verified \\ demi-lunes \\ constructed}" "\specialcell{No. of \\ verified \\ demi-lunes \\ constructed \\ per degraded ha}" "\specialcell{Ratio of verified \\ to total no. \\ of demi-lunes \\ constructed}" "\specialcell{Demi-lunes \\ were operational}") ///
	align(cccccccc) width(\hsize) label collabels(,none) numbers nonotes order(anytreat) keep(anytreat training uct_early cct uct_late) refcat(anytreat "\emph{\underline{Panel A}}" training "\hline\emph{\underline{Panel B}}", nolabel)


	
**********************************;
*TABLE A11: HETEROGENEOUS EFFECTS*;
**********************************;

	use "$data/dlconstruction2018-2020.dta", replace
	est clear

	global hetero "female_l hhsize_l degraded_ha_l demilune_l kantche"

	cap drop het
	gen het = .
	cap drop interact
	gen interact = .
	la var anytreat "Any treatment"
	tab cluster, gen(comm)

	foreach var in $hetero {

		replace het = `var'
		replace interact = anytreat*`var'

		reg interviewer_demilunescounthq0 het interact anytreat vsize admin mobile_l if time==1, r cluster(villID)

		eststo het_`var'

	}

	la var het "Het variable"
	la var interact "Any treat $\times$ Het var"

	// Panel A
	esttab het_* using "$out/tablea11.tex", booktabs ///
	replace label nomtitles nodepvars nonotes noobs wrap b(%10.2fc) se(%10.2fc) star(* 0.10 ** 0.05 *** 0.01) keep(anytreat het interact) ///
	order(anytreat het interact) ///
	refcat(anytreat "\emph{\underline{Panel A: Year 1}}", nolabel) ///
	prehead("{\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi} \begin{tabular}{@{\extracolsep{.1pt}}l*{6}{>{\centering\arraybackslash}m{2cm}}@{}}  \toprule \addlinespace[2mm] \emph{Het var is}: & Female respondent & Adult HH members & Degraded ha owned & DL experience & Kantche \\") ///
	postfoot("")


	foreach var in $hetero {

		replace het = `var'
		replace interact = anytreat*`var'

		reg interviewer_demilunescounthq0 het interact anytreat vsize admin mobile_l if time==3, r cluster(villID)

		eststo het_end_`var'
		
	}

	// Panel B 
	esttab het_end* using "$out/tablea11.tex", booktabs  ///
	append label nomtitles nonumbers nonotes noobs wrap b(%10.2fc) se(%10.2fc) star(* 0.10 ** 0.05 *** 0.01) keep(anytreat het interact) order(anytreat het interact) ///
	subst(\multicolumn{7}) ///
	refcat(anytreat "\emph{\underline{Panel B: Year 3}}", nolabel) ///
	prehead("") 
	
	clear

******************;
*TABLE A12: WAGES*;
******************;

	use "$data/endline_midline_panel.dta"
	est clear

	global wages="dlfamlabor_avpdwage dlnonfamlabor_avpdwage famlaborsold_avpdwage nondllabor_avpdwage"

	local k=1
		foreach i in $wages{   
		xi: reg `i' anytreat kantche vsize admin $tstat i.cluster if time==2, r cluster(villID)
		matrix A = r(table)[1..9,"anytreat"]
		
		qui xi: reg `i' training uct_early cct uct_late kantche vsize admin $tstat i.cluster if time==2, r cluster(villID)
		matrix A = A, r(table)
		estadd matrix bnew = A["b",1.."_cons"]
		estadd matrix se = A["se",1.."_cons"]
		estadd matrix p = A["pvalue",1.."_cons"]

		sum `i' if control==1 & time==1
		estadd sca ctlm = r(mean)
		
		testparm training uct_early, equal
		qui estadd sca pval1=r(p)
		testparm training cct, equal
		qui estadd sca pval2=r(p)
		testparm training uct_late, equal
		qui estadd sca pval3=r(p)
		testparm uct_early cct, equal
		qui estadd sca pval4=r(p)
		testparm uct_early uct_late, equal
		qui estadd sca pval5=r(p)
		testparm cct uct_late, equal
		qui estadd sca pval6=r(p)
		
		eststo C`k'
		local k = `k'+1
		}

		
	la var anytreat "Any treatment"
	la var training "Training"
	la var uct_early "UCT early"
	la var uct_late "UCT late"
	la var cct "CCT"

	esttab C1 C2 C3 C4 using "$out/tablea12.tex", booktabs ///
	replace noobs cells(bnew(fmt(%10.2fc) star pvalue(p)) se(par)) wrap  star(* 0.10 ** 0.05 *** 0.01)  ///
	scalars("ctlm Mean in control" "N No. of observations" "r2 R squared" "pval1 \hline  Training=UCT early" "pval2 Training=CCT" "pval3 Training=UCT late" "pval4 UCT early=CCT" "pval5 UCT early=UCT late" "pval6 CCT=UCT late") sfmt(%10.2fc %10.0f %10.2fc %10.2fc %10.2fc %10.2fc %10.2fc %10.2fc) ///
	mlabels("\specialcell{Average daily wages \\ paid for demi-lunes\\ family labor (CFA)}" "\specialcell{Average daily \\ wages paid \\ for non-family \\ demi-lunes labor}" "\specialcell{Average daily \\ wages earned \\ by family labor \\ sold}" "\specialcell{Average daily \\ wages for \\ non demi-lunes \\ labor (CFA)}") ///
	align(cccccccc) width(\hsize) label collabels(,none) numbers nonotes order(anytreat)	keep(anytreat training uct_early cct uct_late) refcat(anytreat "\emph{\underline{Panel A}}" training "\hline\emph{\underline{Panel B}}", nolabel)
	clear

******************************;
*TABLE A13: LABOR EXPENDITURES*;
******************************;	

	use "$data/endline_midline_panel.dta"
	est clear

	global expenditures="totaldlexpense dlnonfamlabor_expense0 dlfamlabor_expense0 nondllabor_expense0 totallaborexpense famlabor_revenue0"

	local k=1
		foreach i in $expenditures{   
		xi: reg `i' anytreat kantche vsize admin $tstat i.cluster if time==2, r cluster(villID)
		matrix A = r(table)[1..9,"anytreat"]
		
		qui xi: reg `i' training uct_early cct uct_late kantche vsize admin $tstat i.cluster if time==2, r cluster(villID)
		matrix A = A, r(table)
		estadd matrix bnew = A["b",1.."_cons"]
		estadd matrix se = A["se",1.."_cons"]
		estadd matrix p = A["pvalue",1.."_cons"]

		sum `i' if control==1
		estadd sca ctlm = r(mean)
		
		testparm training uct_early, equal
		qui estadd sca pval1=r(p)
		testparm training cct, equal
		qui estadd sca pval2=r(p)
		testparm training uct_late, equal
		qui estadd sca pval3=r(p)
		testparm uct_early cct, equal
		qui estadd sca pval4=r(p)
		testparm uct_early uct_late, equal
		qui estadd sca pval5=r(p)
		testparm cct uct_late, equal
		qui estadd sca pval6=r(p)
		
		eststo C`k'
		local k = `k'+1
		}

		
	la var anytreat "Any treatment"
	la var training "Training"
	la var uct_early "UCT early"
	la var uct_late "UCT late"
	la var cct "CCT"


	esttab C1 C2 C3 C4 C5 C6 using "$out/tablea13.tex", booktabs ///
	replace noobs cells(bnew(fmt(%10.2fc) star pvalue(p)) se(par)) wrap  star(* 0.10 ** 0.05 *** 0.01)  ///
	scalars("ctlm Mean in control" "N No. of observations" "r2 R squared" "pval1 \hline  Training=UCT early" "pval2 Training=CCT" "pval3 Training=UCT late" "pval4 UCT early=CCT" "pval5 UCT early=UCT late" "pval6 CCT=UCT late") sfmt(%10.2fc %10.0f %10.2fc %10.2fc %10.2fc %10.2fc %10.2fc %10.2fc) ///
	mlabels("\specialcell{Total amount \\ spent on \\ labor for \\ demi-lunes}" "\specialcell{Total amount \\ spent on \\ non-family \\ labor \\ for demi-lunes}" "\specialcell{Total amount \\ spent on \\ family \\ labor \\ for demi-lunes}" "\specialcell{Total amount \\ spent on \\ non-demi-lune labor}" "\specialcell{Total amount \\ spent on \\ labor}" "\specialcell{Total revenue \\ from selling \\ family labor}" ) ///
	align(cccccccc) width(\hsize) label collabels(,none) numbers nonotes order(anytreat)	keep(anytreat training uct_early cct uct_late) refcat(anytreat "\emph{\underline{Panel A}}" training "\hline\emph{\underline{Panel B}}", nolabel)
	
******************************
*TABLE A14: PRODUCTION BY ARM*
******************************

	use "$data/endline_midline_panel.dta", replace
	est clear

	gen totalprodkg_y1 = totalprodkg if time == 2
	gen totalprodcfa_y1 = totalprodcfa if time == 2
	gen totalprodkg_y3 = totalprodkg if time == 3
	gen totalprodcfa_y3 = totalprodcfa if time == 3

	global production="totalprodkg_y1 totalprodcfa_y1 totalprodkg_y3 totalprodcfa_y3 "

	local k=1
		foreach i in $production{   
		xi: reg `i' anytreat kantche vsize admin $tstat i.cluster, r cluster(villID)
		matrix A = r(table)[1..9,"anytreat"]
		
		qui xi: reg `i' training uct_early cct uct_late kantche vsize admin $tstat i.cluster, r cluster(villID)
		matrix A = A, r(table)
		estadd matrix bnew = A["b",1.."_cons"]
		estadd matrix se = A["se",1.."_cons"]
		estadd matrix p = A["pvalue",1.."_cons"]

		sum `i' if control==1
		estadd sca ctlm = r(mean)
		
		testparm training uct_early, equal
		qui estadd sca pval1=r(p)
		testparm training cct, equal
		qui estadd sca pval2=r(p)
		testparm training uct_late, equal
		qui estadd sca pval3=r(p)
		testparm uct_early cct, equal
		qui estadd sca pval4=r(p)
		testparm uct_early uct_late, equal
		qui estadd sca pval5=r(p)
		testparm cct uct_late, equal
		qui estadd sca pval6=r(p)
		
		eststo C`k'
		local k = `k'+1
		}

		
	la var anytreat "Any treatment"
	la var training "Training"
	la var uct_early "UCT early"
	la var uct_late "UCT late"
	la var cct "CCT"


	esttab C* using "$out/tablea14.tex", booktabs ///
	replace noobs cells(bnew(fmt(%10.2fc) star pvalue(p)) se(par)) wrap  star(* 0.10 ** 0.05 *** 0.01)  ///
	scalars("ctlm Mean in control" "N No. of observations" "r2 R squared" "pval1 \hline  Training=UCT early" "pval2 Training=CCT" "pval3 Training=UCT late" "pval4 UCT early=CCT" "pval5 UCT early=UCT late" "pval6 CCT=UCT late") sfmt(%10.2fc %10.0f %10.2fc %10.2fc %10.2fc %10.2fc %10.2fc %10.2fc) ///
	mlabels("\specialcell{Total \\ production \\ (kg) year 1}" "\specialcell{Total \\ production \\ (CFA) year 1}" "\specialcell{Total \\ production \\ (kg) year 3}" "\specialcell{Total \\ production \\ (CFA) year 3}") ///
	align(cccc) label collabels(,none) numbers nonotes order(anytreat)	keep(anytreat training uct_early cct uct_late) refcat(anytreat "\emph{\underline{Panel A}}" training "\hline\emph{\underline{Panel B}}", nolabel)



**************************;
*TABLE A15: ASSETS
**************************;	

	use "$data/endline_midline_panel.dta", replace
	est clear

	gen ihsincome=asinh(incomecfa)
	gen ihsnonagincome=asinh(nonagincome_lastharvestcfa)

	global wellbeingmidline="ihsincome asset_index season_food hdds"
	global wellbeingendline = "ihsnonagincome asset_index season_food hdds livestock_nbr"

	la var ihsincome "Inverse hyperbolic sine of income in the past week"
	la var ihsincome "Inverse hypoerbolic sine of non-ag revenues during harvest"
	la var season_nofood "Household did not experience food insecurity over past 12 months"
	la var hdds "Household diet diversity score"
	la var asset_index "Productive asset index"
	la var anytreat "Any treatment"

	local j=1 
	foreach i in $wellbeingmidline {   
		xi: reg `i' anytreat kantche vsize admin $tstat i.cluster if time==2, r cluster(villID)	
		eststo mid`j'
		if e(depvar)=="asset_index" {
			summ assets if anytreat==0 & e(sample) == 1
			estadd scalar conmean_mid = r(mean)
			estadd scalar consd_mid = r(sd)
		}
		else {
			summ `i' if anytreat==0 & e(sample) == 1
			estadd scalar conmean_mid = r(mean)
			estadd scalar consd_mid = r(sd)
		}
		local j=`j'+1
	}

	// Panel A
	esttab mid* using "$out/tablea15.tex", booktabs ///
	replace label nonotes noobs wrap b(%10.2fc) se(%10.2fc) star(* 0.10 ** 0.05 *** 0.01) keep(anytreat) ///
	subst(\begin{tabular}{l*{4}{c} \begin{tabular}{l*{5}{c}) ///
	refcat(anytreat "\emph{\underline{Panel A: Year 1}}", nolabel) ///
	scalars("conmean_mid Mean in control" "N No. of observations" "r2 R squared") ///
	sfmt(%10.2fc %10.0fc %10.2fc) ///
	mlabels("\shortstack{IHS of \\ weekly \\ income}" "\shortstack{Z-score of \\ productive \\ assets}" "\shortstack{HH did not \\ experience \\ food insecurity}" "\shortstack{HH diet \\ diversity \\ score}" ) ///
	postfoot("")


	local j=1 
	foreach i in $wellbeingendline {   	
		xi: reg `i' anytreat kantche vsize admin $tstat i.cluster if time==3, r cluster(villID)
		eststo end`j'
		if e(depvar)=="asset_index" {
			summ assets if anytreat==0 & e(sample) == 1
			estadd scalar conmean_end = r(mean)
			estadd scalar consd_end = r(sd)
		}
		else {
			summ `i' if anytreat==0 & e(sample) == 1
			estadd scalar conmean_end = r(mean)
			estadd scalar consd_end = r(sd)
		}
		local j=`j'+1
	}

	// Panel B
	esttab end* using "$out/tablea15.tex", booktabs ///
	append label nonotes noobs wrap b(%10.2fc) se(%10.2fc) star(* 0.10 ** 0.05 *** 0.01) keep(anytreat) ///
	refcat(anytreat "\emph{\underline{Panel B: Year 3}}", nolabel) ///
	scalars("conmean_end Mean in control" "N No. of observations" "r2 R squared") ///
	sfmt(%10.2fc %10.0fc %10.2fc) ///
	mlabels("\shortstack{IHS of \\ weekly \\ non-ag \\ income}" "\shortstack{Z-score of \\ productive \\ assets}" "\shortstack{HH did not \\ experience \\ food insecurity}" "\shortstack{HH diet \\ diversity \\ score}" "\shortstack{No. of \\ livestock \\ raised}" ) ///
	prehead("\toprule") 

************************;
*TABLE A16: CEA AND CBA*
************************;

	* See text 
	
*******************
*TABLE A17: ANCOVA*
*******************

	est clear

	use "$data/dlconstruction2018-2020.dta", replace
	merge 1:1 menagecode time using "$data/endline_midline_panel.dta", force update replace 
	drop _merge 

	preserve
	use "$data/baseline.dta", clear
	loc ancova "migrate_nbr dlnonfamlabor_used dlfamlabor_used nondllabor_used"
	foreach var in `ancova'{
		gen `var'_b =`var'
	}
	tempfile baseline
	save `baseline'
	restore
	merge m:1 menagecode using  `baseline', force keepusing(migrate_nbr_b nondllabor_used_b  dlnonfamlabor_used_b dlfamlabor_used_b)

	rename interviewer_demilunescounthq0 demilunescounthq0

	loc demilunes "anydemilune demilunescounthq0"
	loc ancova "  migrate_nbr  nondllabor_used cropfailpct2 z_totalprodkg z_totalprodcfa "

	la var cropfailpct2 "Percentage crops failed of crops attempted"
	la var z_totalprodkg "Z-score of production (kg) of crops"
	la var z_totalprodcfa "Z-score of value (CFA) of crops"
	la var testscore "Demi-lune test score"
	la var anydemilune "Constructed any demi-lune"
	la var demilunescounthq0 "No. of verified demi-lunes constructed"
	la var asset_index "Z-score of productive assets"
	la var season_food "HH did not experience food insecurity"
	la var hdds "HH diet diversity score"
	la var soil_mean "Self-reported soil quality"
	la var migrate_nbr "No. of migrants"
	la var nondllabor_used "Hired any non-family non-DL labor"
	la var famlaborsold "Sold family labor"
	la var cultivatenewland "Renewed cultivation on any land"
	la var nolongercultivate "Stopped cultivating any land"

	local sR *
	local sig1 `sR'
	local sig2 `sR'`sR'
	local sig3 `sR'`sR'`sR'

	//Create baseline value of demi-lune outcomes using survey self reports
	bysort menagecode (demilune_lastharvest0_b): replace demilune_lastharvest0_b = demilune_lastharvest0_b[1] if missing(demilune_lastharvest0_b)
	bysort menagecode (demilune_builtno0_b): replace demilune_builtno0_b = demilune_builtno0_b[1] if missing(demilune_builtno0_b)
	gen anydemilune_b = demilune_lastharvest0_b
	gen demilunescounthq0_b = demilune_builtno0_b


	foreach var in `ancova'{
		bysort menagecode (`var'_b): replace `var'_b = `var'_b[1] if missing(`var'_b)

	}

	forvalues i=1(1)3{
		preserve
	 keep if time==`i'
	foreach var of varlist `demilunes' {
	xi: reg `var' anytreat kantche vsize admin $tstat `var'_b i.cluster , r cluster(villID) 
	loc N`var'`i' : di %10.0f e(N)
	sum `var' if anytreat==0 & e(sample)==1
	loc c`var'`i': di %10.2f r(mean)
	loc btreat`var'`i': di %10.2f _b[anytreat]
	local SEtreat`var'`i': di %10.2f _se[anytreat]
	local setreat`var'`i' "(`SEtreat`var'`i'')"

	qui test anytreat
	local streat`var'`i'=cond(`r(p)'<0.01,"`sig3'",cond(`r(p)'<0.05,"`sig2'",cond(`r(p)'<0.1, "`sig1'","")))

	loc name_`var' : var label `var'

	}
	restore

	}


	forvalues i=2(1)3{
		preserve
	 keep if time==`i'
	foreach var of varlist `ancova' {
	xi: reg `var' anytreat kantche vsize admin $tstat `var'_b i.cluster, r cluster(villID)
	loc N`var'`i' : di %10.0f e(N)
	sum `var' if anytreat==0 & e(sample)==1
	loc c`var'`i': di %10.2f r(mean)
	loc btreat`var'`i': di %10.2f _b[anytreat]
	local SEtreat`var'`i': di %10.2f _se[anytreat]
	local setreat`var'`i' "(`SEtreat`var'`i'')"

	qui test anytreat
	local streat`var'`i'=cond(`r(p)'<0.01,"`sig3'",cond(`r(p)'<0.05,"`sig2'",cond(`r(p)'<0.1, "`sig1'","")))

	loc name_`var' : var label `var'

	}
	restore
	}

	texdoc init "$out/tablea17.tex" , replace force 

	tex \maxsizebox*{\hsize}{\textheight}{
	tex \begin{tabular}{lSSSSSS}
	tex \toprule
	tex  &  \multicolumn{3}{c}{Year 1} & \multicolumn{3}{c}{Year 3}  \\\cmidrule(lr){2-4}\cmidrule(lr){5-7}
	tex  & \multicolumn{6}{c}{} \\
	tex &  \multicolumn{1}{c}{Control mean} & \multicolumn{1}{c}{Any treatment} & \multicolumn{1}{c}{N} & \multicolumn{1}{c}{Control mean} & \multicolumn{1}{c}{Any treatment} & \multicolumn{1}{c}{N}  \\
	tex &   \multicolumn{1}{c}{(1)} & \multicolumn{1}{c}{(2)} & \multicolumn{1}{c}{(3)}  & \multicolumn{1}{c}{(4)} & \multicolumn{1}{c}{(5)} & \multicolumn{1}{c}{(6)} \\
	tex \midrule
	tex & & \\

	foreach var of varlist `demilunes' { 
	tex `name_`var'' & `c`var'1' & `btreat`var'1'$^{`streat`var'1'}$ & `N`var'1' & `c`var'3' & `btreat`var'3'$^{`streat`var'3'}$ & `N`var'3' \\
	tex              &  &  `setreat`var'1' & & & `setreat`var'3'  &    \\
	}

	foreach var of varlist `ancova' { 
	tex `name_`var'' & `c`var'2'& `btreat`var'2'$^{`streat`var'2'}$ & `N`var'2'&`c`var'3' & `btreat`var'3'$^{`streat`var'3'}$ & `N`var'3' \\
	tex             & &  `setreat`var'2' &  & & `setreat`var'3'  &    \\
	}
	tex \bottomrule
	tex \end{tabular}
	tex }
	tex \\ 

	texdoc close

***********************
*TABLE A18: LEE BOUNDS*
***********************

	use "$data/endline_midline_panel.dta", replace

	loc leevars2 " migrate_nbr nondllabor_used famlaborsold cropfailpct2  z_totalprodkg z_totalprodcfa soil_mean "
	loc leevars3 "  migrate_nbr nondllabor_used famlaborsold cropfailpct2  z_totalprodkg z_totalprodcfa cultivatenewland nolongercultivate"

	la var cropfailpct2 "Percentage crops failed of crops attempted"
	la var z_totalprodkg "Z-score of production (kg) of crops"
	la var z_totalprodcfa "Z-score of value (CFA) of crops"
	la var testscore "Demi-lune test score"
	la var asset_index "Z-score of productive assets"
	la var season_food "HH did not experience food insecurity"
	la var hdds "HH diet diversity score"
	la var soil_mean "Self-reported soil quality"
	la var migrate_nbr "No. of migrants"
	la var nondllabor_used "Hired any non-family non-DL labor"
	la var famlaborsold "Sold family labor"
	la var cultivatenewland "Renewed cultivation on any land"
	la var nolongercultivate "Stopped cultivating any land"

	local ag cropfailpct2  z_totalprodkg z_totalprodcfa 
	local land soil_mean cultivatenewland nolongercultivate
	local labour  migrate_nbr nondllabor_used famlaborsold

	/* Set Locals for Significance stars */
	local sR *
	local sig1 `sR'
	local sig2 `sR'`sR'
	local sig3 `sR'`sR'`sR'

	forvalues i=2(1)3{
		preserve
	 keep if time==`i'
	foreach var of varlist `leevars`i'' {
	xi: reg `var' anytreat vsize admin $tstat i.cluster, r cluster(villID)

	loc btreat`var'`i': di %10.2f _b[anytreat]
	local SEtreat`var'`i': di %10.2f _se[anytreat]
	local setreat`var'`i' "(`SEtreat`var'`i'')"

	qui test anytreat
	local streat`var'`i'=cond(`r(p)'<0.01,"`sig3'",cond(`r(p)'<0.05,"`sig2'",cond(`r(p)'<0.1, "`sig1'","")))

	leebounds `var' anytreat, tight(kantche female_l)  vce(bootstrap)
	local blower`var'`i': di %10.2f e(b)[1,1]
	local bupper`var'`i': di %10.2f e(b)[1,2]
	local SElower`var'`i': di %10.2f r(table)[2,1]
	local SEupper`var'`i':di %10.2f r(table)[2,2]
	local plower`var'`i':di %10.2f  r(table)[4,1]
	local pupper`var'`i': di %10.2f r(table)[4,2]
		
	local selower`var'`i' "(`SElower`var'`i'')"
	local seupper`var'`i' "(`SEupper`var'`i'')"
	local slower`var'`i'=cond(`plower`var'`i''<0.01,"`sig3'",cond(`plower`var'`i''<0.05,"`sig2'",cond(`plower`var'`i''<0.1, "`sig1'","")))
	local supper`var'`i'=cond(`pupper`var'`i''<0.01,"`sig3'",cond(`pupper`var'`i''<0.05,"`sig2'",cond(`pupper`var'`i''<0.1, "`sig1'","")))
	loc name_`var' : var label `var'

	}
	restore
	}

	texdoc init "$out/tablea18.tex" , replace force 

	tex \maxsizebox*{\hsize}{\textheight}{
	tex \begin{tabular}{lSSSSSSS}
	tex \toprule
	tex  & \multicolumn{3}{c}{Year 1} & \multicolumn{3}{c}{Year 3}  \\\cmidrule(lr){2-4}\cmidrule(lr){5-7}
	tex  & \multicolumn{6}{c}{} \\
	tex &  \multicolumn{1}{c}{Any treatment} & \multicolumn{1}{c}{Lower bound} & \multicolumn{1}{c}{Upper bound} & \multicolumn{1}{c}{Any treatment} & \multicolumn{1}{c}{Lower bound} & \multicolumn{1}{c}{Upper bound} \\
	tex &   \multicolumn{1}{c}{(1)} & \multicolumn{1}{c}{(2)} & \multicolumn{1}{c}{(3)}  & \multicolumn{1}{c}{(4)} & \multicolumn{1}{c}{(5)} & \multicolumn{1}{c}{(6)} \\
	tex \midrule
	tex  \\

	foreach var of varlist `labour' { 
	tex `name_`var'' & `btreat`var'2'$^{`streat`var'2'}$ & `blower`var'2'$^{`slower`var'2'}$ & `bupper`var'2'$^{`supper`var'2'}$ & `btreat`var'3'$^{`streat`var'3'}$ & `blower`var'3'$^{`slower`var'3'}$ & `bupper`var'3'$^{`supper`var'3'}$ \\
	tex              &   `setreat`var'2' &   `selower`var'2' &   `seupper`var'2'  &   `setreat`var'3' &   `selower`var'3' &   `seupper`var'3' \\
	}
	//tex & & \\

	foreach var of varlist `ag' { 
	tex `name_`var'' & `btreat`var'2'$^{`streat`var'2'}$ & `blower`var'2'$^{`slower`var'2'}$ & `bupper`var'2'$^{`supper`var'2'}$ & `btreat`var'3'$^{`streat`var'3'}$ & `blower`var'3'$^{`slower`var'3'}$ & `bupper`var'3'$^{`supper`var'3'}$ \\
	tex              &   `setreat`var'2' &   `selower`var'2' &   `seupper`var'2'  &   `setreat`var'3' &   `selower`var'3' &   `seupper`var'3' \\
	}

	foreach var of varlist `land' { 
	tex `name_`var'' & `btreat`var'2'$^{`streat`var'2'}$ & `blower`var'2'$^{`slower`var'2'}$ & `bupper`var'2'$^{`supper`var'2'}$ & `btreat`var'3'$^{`streat`var'3'}$ & `blower`var'3'$^{`slower`var'3'}$ & `bupper`var'3'$^{`supper`var'3'}$ \\
	tex              &   `setreat`var'2' &   `selower`var'2' &   `seupper`var'2'  &   `setreat`var'3' &   `selower`var'3' &   `seupper`var'3' \\
	}

	tex \bottomrule
	tex \end{tabular}
	tex }
	tex \\ 

	texdoc close

	

****************
*TABLE A19: MHT*
****************


	/* Set locals for significance stars */
	local sR *
	local sig1 `sR'
	local sig2 `sR'`sR'
	local sig3 `sR'`sR'`sR'

	global dt = "$data/temp"


	*===============================================================================
	use "$data/dlconstruction2018-2020.dta", replace

	global demilunes="anydemilune  interviewer_demilunescounthq0 dlperha dlqualityratio interviewer_demilunessowed0 interviewer_intrants0"


	la var anydemilune "Constructed any demi-lune" 
	la var interviewer_demilunescounthq0 "No. of verified demi-lunes constructed" 
	la var dlperha "No. of verified demi-lunes constructed per degraded ha" 
	la var dlqualityratio "Ratio of verified to total no.of demi-lunes constructed" 
	la var interviewer_demilunessowed0 "Sowed in demi-lunes" 
	la var interviewer_intrants0 "Used manure in demi-lunes"

	gen pval=.
	gen btreat=.
	gen SEtreat=.
	gen str index=""
	gen str var_name=""

	local j 1

	foreach y in $demilunes{
		replace index="`y'" if _n==`j'
		local label_var: var label `y'
		replace var_name= "`label_var'"  if _n==`j'
		xi: reg `y' anytreat kantche vsize admin $tstat i.cluster if time==1, r cluster(villID)
		replace btreat=_b[anytreat] if _n == `j'
		replace SEtreat= _se[anytreat] if _n == `j'
		test anytreat
		replace pval= `r(p)' if _n == `j'
		local j=`j'+1
	}

	keep  index pval btreat SEtreat var_name

	keep if index!=""
	save "$dt/pval", replace


	*===============================================================================
	use "$data/endline_midline_panel.dta", replace

	global labormidline="dlfamlabor_persondays0 dlnonfamlabor_persondays0 migrate_nbr famlaborsold_nbr0 nondllabor_used"
	global laborendline="migrate_nbr famlaborsold_nbr0 nondllabor_used"

	la var anytreat "Any treatment"

	estimates clear
	gen pval=.
	gen btreat=.
	gen SEtreat=.
	gen str index=""
	gen str var_name=""

	local j=1 // counter for storing estimates
	foreach y in $labormidline { 
		replace index="`y'" if _n==`j' 
		local label_var: var label `y'
		replace var_name= "`label_var'"  if _n==`j'
		xi: reg `y' anytreat kantche vsize admin $tstat i.cluster if time==2, r cluster(villID)	
		replace btreat=_b[anytreat] if _n == `j'
		replace SEtreat= _se[anytreat] if _n == `j'
		test anytreat
		replace pval=  `r(p)' if _n == `j'
		local j=`j'+1
		}
		
	keep  index pval btreat SEtreat var_name

	keep if index!=""

	append using "$dt/pval"
	save "$dt/pval", replace
		
	use "$data/endline_midline_panel.dta", replace

	global labormidline="dlfamlabor_persondays0 dlnonfamlabor_persondays0 migrate_nbr famlaborsold_nbr0 nondllabor_used"
	global laborendline="migrate_nbr famlaborsold_nbr0 nondllabor_used"

	la var anytreat "Any treatment"

	estimates clear
	gen pval=.
	gen btreat=.
	gen SEtreat=.
	gen str index=""
	gen str var_name=""

	local j=1 // counter for storing estimates

	foreach y in $laborendline {  
		replace index="`y'_end" if _n==`j' 
		local label_var: var label `y'
		replace var_name= "`label_var'"  if _n==`j'
		xi: reg `y' anytreat kantche vsize admin $tstat i.cluster if time==3, r cluster(villID)	
		replace btreat=_b[anytreat] if _n == `j'
		replace SEtreat= _se[anytreat] if _n == `j'
		test anytreat
		replace pval=  `r(p)' if _n == `j'
		local j=`j'+1
		}
		
	keep  index pval btreat SEtreat var_name

	keep if index!=""

	append using "$dt/pval"
	save "$dt/pval", replace
		

	*===============================================================================
	use "$data/endline_midline_panel.dta", replace

	global productionmidline="cropattempt_nbr cropfailpct2 z_totalprodkg z_totalprodcfa"
	global productionendline="cropattempt_nbr cropfailpct2 z_totalprodkg z_totalprodcfa"

	la var anytreat "Any treatment"

	estimates clear
	gen pval=.
	gen btreat=.
	gen SEtreat=.
	gen str index=""
	gen str var_name=""

	local j=1 // counter for storing estimates
	foreach y in $productionmidline {  
		replace index="`y'" if _n==`j' 
		local label_var: var label `y'
		replace var_name= "`label_var'"  if _n==`j'
		xi: reg `y' anytreat kantche vsize admin $tstat i.cluster if time==2, r cluster(villID)
		replace btreat=_b[anytreat] if _n == `j'
		replace SEtreat= _se[anytreat] if _n == `j'
		test anytreat
		replace pval=  `r(p)' if _n == `j'
		local j=`j'+1
		}
		
	keep  index pval btreat SEtreat var_name

	keep if index!=""

	append using "$dt/pval"
	save "$dt/pval", replace

	use "$data/endline_midline_panel.dta", replace

	global productionmidline="cropattempt_nbr cropfailpct2 z_totalprodkg z_totalprodcfa"
	global productionendline="cropattempt_nbr cropfailpct2 z_totalprodkg z_totalprodcfa"

	la var anytreat "Any treatment"

	estimates clear
	gen pval=.
	gen btreat=.
	gen SEtreat=.
	gen str index=""
	gen str var_name=""

	local j=1 // counter for storing estimates
	foreach y in $productionendline {  
		replace index="`y'_end" if _n==`j' 
		local label_var: var label `y'
		replace var_name= "`label_var'"  if _n==`j'
		xi: reg `y' anytreat kantche vsize admin $tstat i.cluster if time==3, r cluster(villID)	
		replace btreat=_b[anytreat] if _n == `j'
		replace SEtreat= _se[anytreat] if _n == `j'
		test anytreat
		replace pval=  `r(p)' if _n == `j'
		local j=`j'+1
		}
		
	keep  index pval btreat SEtreat var_name

	keep if index!=""

	append using "$dt/pval"
	save "$dt/pval", replace


	*===============================================================================
	use "$data/dlconstruction2018-2020.dta", replace

	merge 1:1 menagecode time using "$data/endline_midline_panel.dta", force update replace // note that any overlapping varnames will be overwritten
	tab _merge
	drop _merge 
	winsor2 totalhectares_degraded, cuts(1 99) by(anytreat) suffix(_w2)


	global landmidline="fields_number cultivatelands2017 degradedcult_ha soil_mean"
	global landendline = "fields_number cultivatenewland nolongercultivate soilqualityimprove"

	la var anytreat "Any treatment"

	estimates clear
	gen pval=.
	gen btreat=.
	gen SEtreat=.
	gen str index=""
	gen str var_name=""

	local j=1 // counter for storing estimates
	foreach y in $landmidline {
		replace index="`y'" if _n==`j' 
		local label_var: var label `y'
		replace var_name= "`label_var'"  if _n==`j'
		xi: reg `y' anytreat kantche vsize admin $tstat i.cluster if time==2, r cluster(villID)
		replace btreat=_b[anytreat] if _n == `j'
		replace SEtreat= _se[anytreat] if _n == `j'
		test anytreat
		replace pval=  `r(p)' if _n == `j'
		local j=`j'+1
		}
		
	keep  index pval btreat SEtreat var_name

	keep if index!=""

	append using "$dt/pval"
	save "$dt/pval", replace

	use "$data/dlconstruction2018-2020.dta", replace

	merge 1:1 menagecode time using "$data/endline_midline_panel.dta", force update replace // note that any overlapping varnames will be overwritten
	tab _merge
	drop _merge 
	winsor2 totalhectares_degraded, cuts(1 99) by(anytreat) suffix(_w2)

	global landmidline="fields_number cultivatelands2017 degradedcult_ha soil_mean"
	global landendline = "fields_number cultivatenewland nolongercultivate soilqualityimprove"

	la var anytreat "Any treatment"

	estimates clear
	gen pval=.
	gen btreat=.
	gen SEtreat=.
	gen str index=""
	gen str var_name=""

	local j=1 // counter for storing estimates
	foreach y in $landendline {
		replace index="`y'_end" if _n==`j' 
		local label_var: var label `y'
		replace var_name= "`label_var'"  if _n==`j'
		xi: reg `y' anytreat kantche vsize admin $tstat i.cluster if time==3, r cluster(villID)	
		replace btreat=_b[anytreat] if _n == `j'
		replace SEtreat= _se[anytreat] if _n == `j'
		test anytreat
		replace pval=  `r(p)' if _n == `j'
		local j=`j'+1
		}
		
	keep  index pval btreat SEtreat var_name

	keep if index!=""

	append using "$dt/pval"
	save "$dt/pval", replace
		

	* Collect the total number of p-values tested
	use "$dt/pval", clear
	quietly sum pval
	local totalpvals = r(N)

	* Sort the p-values in ascending order and generate a variable that codes each p-value's rank

	quietly gen int original_sorting_order = _n
	quietly sort pval
	quietly gen int rank = _n if pval~=.

	* Set the initial counter to 1 

	local qval = 1

	* Generate the variable that will contain the BKY (2006) sharpened q-values

	gen bky06_qval = 1 if pval~=.

	* Set up a loop that begins by checking which hypotheses are rejected at q = 1.000, then checks which hypotheses are rejected at q = 0.999, then checks which hypotheses are rejected at q = 0.998, etc.  The loop ends by checking which hypotheses are rejected at q = 0.001.


	while `qval' > 0 {
		* First Stage
		* Generate the adjusted first stage q level we are testing: q' = q/1+q
		local qval_adj = `qval'/(1+`qval')
		* Generate value q'*r/M
		gen fdr_temp1 = `qval_adj'*rank/`totalpvals'
		* Generate binary variable checking condition p(r) <= q'*r/M
		gen reject_temp1 = (fdr_temp1>=pval) if pval~=.
		* Generate variable containing p-value ranks for all p-values that meet above condition
		gen reject_rank1 = reject_temp1*rank
		* Record the rank of the largest p-value that meets above condition
		egen total_rejected1 = max(reject_rank1)

		* Second Stage
		* Generate the second stage q level that accounts for hypotheses rejected in first stage: q_2st = q'*(M/m0)
		local qval_2st = `qval_adj'*(`totalpvals'/(`totalpvals'-total_rejected1[1]))
		* Generate value q_2st*r/M
		gen fdr_temp2 = `qval_2st'*rank/`totalpvals'
		* Generate binary variable checking condition p(r) <= q_2st*r/M
		gen reject_temp2 = (fdr_temp2>=pval) if pval~=.
		* Generate variable containing p-value ranks for all p-values that meet above condition
		gen reject_rank2 = reject_temp2*rank
		* Record the rank of the largest p-value that meets above condition
		egen total_rejected2 = max(reject_rank2)

		* A p-value has been rejected at level q if its rank is less than or equal to the rank of the max p-value that meets the above condition
		replace bky06_qval = `qval' if rank <= total_rejected2 & rank~=.
		* Reduce q by 0.001 and repeat loop
		drop fdr_temp* reject_temp* reject_rank* total_rejected*
		local qval = `qval' - .001
	}
		
	quietly sort original_sorting_order
	pause off
	*set more on

	display "Code has completed."
	display "Benjamini Krieger Yekutieli (2006) sharpened q-vals are in variable 'bky06_qval'"
	display	"Sorting order is the same as the original vector of p-values"

	* Note: Sharpened FDR q-vals can be LESS than unadjusted p-vals when many hypotheses are rejected, because if you have many true rejections, then you can tolerate several false rejections too (this effectively just happens for p-vals that are so large that you are not going to reject them regardless).
	save "$dt/pval_qval", replace
	use "$dt/pval_qval", clear

	replace var_name = "Constructed any DL (Year 1)" if index == "anydemilune"
	replace var_name = "No. of verified DL constructed (Year 1)" if index == "interviewer_demilunescounthq0" 
	replace var_name = "No. of verified DL constructed per degraded ha (Year 1)" if index == "dlperha" 
	replace var_name = "Ratio of verified to total no. of DL constructed (Year 1)" if index == "dlqualityratio" 
	replace var_name = "Sowed in DL (Year 1)" if index == "interviewer_demilunessowed0" 
	replace var_name = "Used manure in DL (Year 1)" if index == "interviewer_intrants0" 
	*replace var_name = "Any DL adopted on neighbors' plots'" if index == "demilune_last3" 
	*replace var_name = "No. of fields where neighbors' adopted'" if index == "num_dl_tot" 
	*replace var_name = "" if index == "cultivatenewland_ha0" 
	*replace var_name = "" if index == "asset_index" 
	*replace var_name = "" if index == "testscore" 
	replace var_name = "Person-days of DL family labor used (Year 1)" if index == "dlfamlabor_persondays0" 
	replace var_name = "Person-days of DL non-family labor used (Year 1)" if index == "dlnonfamlabor_persondays0" 
	replace var_name = "No. of migrants (Year 1)" if index == "migrate_nbr" 
	replace var_name = "No. of family members selling labor (Year 1)" if index == "famlaborsold_nbr0" 
	replace var_name = "Hired any non-family non-DL labor (Year 1)" if index == "nondllabor_used" 
	replace var_name = "No. of migrants (Year 3)" if index == "migrate_nbr_end" 
	replace var_name = "No. of family members selling labor (Year 3)" if index == "famlaborsold_nbr0_end" 
	replace var_name = "Hired any non-family non-DL labor (Year 3)" if index == "nondllabor_used_end" 
	replace var_name = "No. of crops planted (Year 1)" if index == "cropattempt_nbr" 
	replace var_name = "Percentage of crops failed of crops attempted (Year 1)" if index == "cropfailpct2" 
	replace var_name = "Z-score of production (kg) of crops (Year 1)" if index == "z_totalprodkg" 
	replace var_name = "Z-score of value (CFA) of crops (Year 1)" if index == "z_totalprodcfa" 
	replace var_name = "No. of crops planted (Year 3)" if index == "cropattempt_nbr_end" 
	replace var_name = "Percentage of crops failed of crops attempted (Year 3)" if index == "cropfailpct2_end" 
	replace var_name = "Z-score of production (kg) of crops (Year 3)" if index == "z_totalprodkg_end" 
	replace var_name = "Z-score of value (CFA) of crops (Year 3)" if index == "z_totalprodcfa_end" 
	replace var_name = "No. of fields owned or rented (Year 1)" if index == "fields_number" 
	replace var_name = "Ha. of land cultivated (Year 1)" if index == "cultivatelands2017" 
	replace var_name = "Ha. of degraded land cultivated (Year 1)" if index == "degradedcult_ha" 
	replace var_name = "Self-reported soil quality (Year 1)" if index == "soil_mean" 
	replace var_name = "No. of fields owned or rented (Year 3)" if index == "fields_number_end" 
	replace var_name = "Renewed cultivation on any land (Year 3)" if index == "cultivatenewland_end" 
	replace var_name = "Stopped cultivating any land (Year 3)" if index == "nolongercultivate_end" 
	replace var_name = "Soil quality improved (Year 3)" if index == "soilqualityimprove_end" 


	local mht "anydemilune  interviewer_demilunescounthq0 dlperha dlqualityratio interviewer_demilunessowed0 interviewer_intrants0 dlfamlabor_persondays0 dlnonfamlabor_persondays0 migrate_nbr famlaborsold_nbr0 nondllabor_used migrate_nbr_end famlaborsold_nbr0_end nondllabor_used_end cropattempt_nbr cropfailpct2 z_totalprodkg z_totalprodcfa cropattempt_nbr_end cropfailpct2_end z_totalprodkg_end z_totalprodcfa_end fields_number cultivatelands2017 degradedcult_ha soil_mean fields_number_end cultivatenewland_end  nolongercultivate_end soilqualityimprove_end "

	local i=1
	gen order = .
	foreach y in `mht'{
		replace order = `i' if index == "`y'"
	local i=`i'+1
	}
	sort order

	local i=1
	foreach y in `mht'{
		sum pval if index=="`y'", d
		local pval_unad`i':  di %-4.2f r(mean)
		local p`i' "(`pval_unad`i'')"
		sum bky06_qval if index=="`y'",d
		local pval_adjusted`i':  di %-4.2f r(mean)
		local p_adjusted`i' "(`pval_adjusted`i'')"
		sum btreat if index=="`y'",d
		local btreat`i':  di %-4.3f r(mean)
		sum SEtreat if index=="`y'",d
		local SEtreat`i':  di %-4.2f r(mean)
		local setreat`i' "(`SEtreat`i'')"
	local streat`i'=cond(`pval_unad`i''<0.01,"`sig3'",cond(`pval_unad`i''<0.05,"`sig2'",cond(`pval_unad`i''<0.1, "`sig1'","")))
	 //local name_`i'= var_name  if index=="`y'"
	 local name_`i'=var_name[`i']
		 
	local i=`i'+1
	}

	/* Write Latex Table */
	texdoc init "$out/tablea19.tex" , replace force 

	tex {
	*tex \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
	tex \begin{tabular}{lccc}
	tex \toprule
	tex &  Any & Unadjusted & Adjusted \\
	tex & treatment & p-val & p-val \\
	tex &   (1) & (2) & (3)  \\
	tex \midrule
	tex & & & \\


	forval i = 1/30 { 	
	tex `name_`i'' & `btreat`i''$^{`streat`i''}$ & `p`i'' & `p_adjusted`i'' \\
	*tex  & `setreati' &  &  \\

	}

	tex \bottomrule
	tex \end{tabular}
	tex }

	texdoc close

//////////////////////
// APPENDIX FIGURES // 
//////////////////////

************************
*FIGURE A1: AG CALENDAR*
************************

	* See text
	

******************
*FIGURE A2: DESIGN*
******************

	* See text

	
********************************;
*FIGURE A3: AGRICULTURE BY CROP*;
********************************;

	use "$data/endline_midline_panel.dta", replace
	est clear
	
	global productionkg = "milqtykg sorghoqtykg cowpeaqtykg arachqtykg sesaqtykg totalprodkg totalprodcfa"

	gen anytreat2019 = (anytreat==1 & time==2)
	gen anytreat2021 = (anytreat==1 & time==3)

	gen group = 1 if anytreat == 0 & time == 2
	replace group = 2 if anytreat == 1 & time == 2
	replace group = 3 if anytreat == 0 & time == 3
	replace group = 4 if anytreat == 1 & time == 3

	gen graph_cat = 1 if group==1
	replace graph_cat = 2.25 if group==2
	replace graph_cat = 4 if group==3
	replace graph_cat = 5.25 if group==4

	gen y=.
	gen hiy=.
	gen lowy=.

	foreach i in milqtykg sorghoqtykg {
		di "`i'"
		if "`i'"== "milqtykg" {
			local k = "Qty of millet produced"
			local price = 174
			local j = "Millet"
		}
		else if "`i'"== "sorghoqtykg" {
			local k = "Qty of sorghum produced"
			local price = 179
			local j = "Sorghum"
		}
		
		sum `i' if anytreat==1 & time==2 & attrition==0
		local midN = r(N)
		
		xi: reg `i' anytreat kantche vsize admin $tstat i.cluster if time==2, r cluster(villID)
		local pval_mid = r(table)[4,1]
		if `pval_mid'>0.1 {
			local star_mid = "" 
		}
		else if `pval_mid'>0.05 & `pval_mid'<=0.1 {
			local star_mid = "*"
		}
		else if `pval_mid'>0.01 & `pval_mid'<=0.05 {
			local star_mid = "**"
		}
		else if `pval_mid'<=0.01 {
			local star_mid = "***"
		}
		sum `i' if anytreat==0 & time==2
		local mid_controlN = r(N)
		replace y = r(mean) if anytreat==0 & time==2
		replace y = r(mean) + _b[anytreat] if anytreat==1 & time==2 
		replace hiy = r(mean) + _b[anytreat] + 1.96*(_se[anytreat]) if anytreat==1 & time==2
		replace lowy = r(mean) + _b[anytreat] - 1.96*(_se[anytreat]) if anytreat==1 & time==2
		
		
		sum `i' if anytreat==1 & time==3 & attrition==0
		local endN = r(N)
		
		xi: reg `i' anytreat kantche vsize admin $tstat i.cluster if time==3, r cluster(villID)
		local pval_end = r(table)[4,1]
		if `pval_end'>0.1 {
			local star_end = "" 
		}
		else if `pval_end'>0.05 & `pval_end'<=0.1 {
			local star_end = "*"
		}
		else if `pval_end'>0.01 & `pval_end'<=0.05 {
			local star_end = "**"
		}
		else if `pval_end'<=0.01 {
			local star_end = "***"
		}
		sum `i' if anytreat==0 & time==3
		local end_controlN = r(N)
		replace y = r(mean) if anytreat==0 & time==3
		replace y = r(mean) + _b[anytreat] if anytreat==1 & time==3 
		replace hiy = r(mean) + _b[anytreat] + 1.96*(_se[anytreat]) if anytreat==1 & time==3
		replace lowy = r(mean) + _b[anytreat] - 1.96*(_se[anytreat]) if anytreat==1 & time==3
		
		preserve
		
		collapse y hiy lowy graph_cat, by(group)
		
		graph twoway (bar y graph_cat if group==1, color(black) lcolor(black)) ///
		 (bar y  graph_cat if group==2, color(gs15) lcolor(black)) ///
		 (bar y  graph_cat if group==3, color(black) lcolor(black)) ///
		 (bar y  graph_cat if group==4, color(gs15) lcolor(black)) ///
		 (rcap hiy lowy graph_cat), ///
		legend(row(1) order (1 "Control" 2 "Any treat") size(small) symxsize(*.35)) ///
		xtitle("Year 1                           Year 3") ///
		xlabel(1 "(N = `mid_controlN')" 2.25 "(N = `midN')`star_mid'" 4 "(N = `end_controlN')" 5.25 "(N = `endN')`star_end'", noticks labsize(small)) ///
		ytitle("`k'") ymtick(##4) ylabel(0(100)600, nogrid) ///
		title("`j'", size(medsmall) color(black)) subtitle("`price' CFA/kg", size(small) color(black))
		graph rename p_`i', replace
		
		restore
		
	}

	foreach i in cowpeaqtykg arachqtykg sesaqtykg {
		di "`i'"
		if "`i'"== "cowpeaqtykg" {
			local k = "Qty of cowpea produced"
			local price = 250
			local j = "Cowpea"
		}
		else if "`i'"== "arachqtykg" {
			local k = "Qty of peanut produced"
			local price = 370
			local j = "Peanut"
		}
		else if "`i'"== "sesaqtykg" {
			local k = "Qty of sesame produced"
			local price = 500
			local j = "Sesame"
		} 

		sum `i' if anytreat==1 & time==2 & attrition==0
		local midN = r(N)
		
		xi: reg `i' anytreat kantche vsize admin $tstat i.cluster if time==2, r cluster(villID)
		local pval_mid = r(table)[4,1]
		if `pval_mid'>0.1 {
			local star_mid = "" 
		}
		else if `pval_mid'>0.05 & `pval_mid'<=0.1 {
			local star_mid = "*"
		}
		else if `pval_mid'>0.01 & `pval_mid'<=0.05 {
			local star_mid = "**"
		}
		else if `pval_mid'<=0.01 {
			local star_mid = "***"
		}
		sum `i' if anytreat==0 & time==2
		local mid_controlN = r(N)
		replace y = r(mean) if anytreat==0 & time==2
		replace y = r(mean) + _b[anytreat] if anytreat==1 & time==2 
		replace hiy = r(mean) + _b[anytreat] + 1.96*(_se[anytreat]) if anytreat==1 & time==2
		replace lowy = r(mean) + _b[anytreat] - 1.96*(_se[anytreat]) if anytreat==1 & time==2
		

		sum `i' if anytreat==1 & time==3 & attrition==0
		local endN = r(N)
		
		xi: reg `i' anytreat kantche vsize admin $tstat i.cluster if time==3, r cluster(villID)
		local pval_end = r(table)[4,1]
		if `pval_end'>0.1 {
			local star_end = "" 
		}
		else if `pval_end'>0.05 & `pval_end'<=0.1 {
			local star_end = "*"
		}
		else if `pval_end'>0.01 & `pval_end'<=0.05 {
			local star_end = "**"
		}
		else if `pval_end'<=0.01 {
			local star_end = "***"
		}
		sum `i' if anytreat==0 & time==3
		local end_controlN = r(N)
		replace y = r(mean) if anytreat==0 & time==3
		replace y = r(mean) + _b[anytreat] if anytreat==1 & time==3 
		replace hiy = r(mean) + _b[anytreat] + 1.96*(_se[anytreat]) if anytreat==1 & time==3
		replace lowy = r(mean) + _b[anytreat] - 1.96*(_se[anytreat]) if anytreat==1 & time==3
		
		preserve
		
		collapse y hiy lowy graph_cat, by(group)
		
		graph twoway (bar y graph_cat if group==1, color(black) lcolor(black)) ///
		 (bar y  graph_cat if group==2, color(gs15) lcolor(black)) ///
		 (bar y  graph_cat if group==3, color(black) lcolor(black)) ///
		 (bar y  graph_cat if group==4, color(gs15) lcolor(black)) ///
		 (rcap hiy lowy graph_cat), ///
		legend(row(1) order (1 "Control" 2 "Any treat") size(small) symxsize(*.4)) ///
		xtitle("Year 1                      Year 3") ///
		xlabel(1 "(N = `mid_controlN')" 2.25 "(N = `midN')`star_mid'" 4 "(N = `end_controlN')" 5.25 "(N = `endN')`star_end'", noticks labsize(small) ) ///
		ytitle("`k'") ymtick(##4) ylabel(0(20)120, nogrid) ///
		title("`j'", size(medsmall) color(black)) subtitle("`price' CFA/kg", size(small) color(black))
		graph rename p_`i', replace
		
		restore
	}

	graph combine p_milqtykg p_sorghoqtykg, ycommon graphregion(color(white) margin(l=22 r=22)) 
	graph rename p1, replace

	graph combine p_cowpeaqtykg p_arachqtykg p_sesaqtykg, rows(1) graphregion(color(white))
	graph rename p2, replace

	graph combine p1 p2, rows(2)

	graph export "$out/figa3.png", replace



******************;
*FIGURE A4: ASSETS*;
******************;

	use "$data/endline_midline_panel.dta", replace
	est clear

	global assets owns_electric owns_velo owns_moto owns_charette owns_charrue owns_pelle owns_pioche owns_cell

	foreach i in $assets{
		gen `i'_treat=anytreat 
	}
		
	foreach i in $assets{
		quietly xi: reg `i' `i'_treat kantche vsize admin $tstat i.cluster if time==2, r cluster(villagecode)
		estimates store `i'_2
		quietly xi: reg `i' `i'_treat kantche vsize admin $tstat i.cluster if time==3, r cluster(villagecode)
		estimates store `i'_3
	}

	la var owns_electric_treat "Owns generator"
	la var owns_velo_treat "Owns bike"
	la var owns_moto_treat "Owns moto"
	la var owns_charette_treat "Owns cart"
	la var owns_charrue_treat "Owns plow"
	la var owns_pelle_treat "Owns shovel"
	la var owns_pioche_treat "Owns pickaxe"
	la var owns_cell_treat "Owns cell phone"	

	coefplot	(owns_electric_2 owns_velo_2 owns_moto_2 owns_charette_2 owns_charrue_2 owns_pelle_2 owns_pioche_2 owns_cell_2, label(Year 1)) ///
				(owns_electric_3 owns_velo_3 owns_moto_3 owns_charette_3 owns_charrue_3 owns_pelle_3 owns_pioche_3 owns_cell_3, label(Year 3)), ///
	keep (owns_*_treat) xline(0) xtitle(Any Treat, size(medsmall)) legend() graphregion(margin(l=75)) yscale(alt noline) ///
	coeflabels(, labsize(small) labgap(-80) notick ) mlcolor(black) graphregion(fcolor(white)) 
				
	graph export "$out/figa4.png", replace
	

****************************************
*FIGURE A5: SOIL QUALITY, SELF-REPORTED*
****************************************

	use "$data/endline_midline_panel.dta", replace

	foreach i in soilqualityimprove vegetationreturned waterretention soilqualitydegrade  vegetationdisappeared {

	replace `i' = 0 if `i' == . & noticesoilchange != .
	gen `i'_treat=anytreat 

	xi: reg `i' `i'_treat kantche vsize admin i.cluster $tstat, r cluster(villagecode)
	estimates store `i'
	}

	la var soilqualityimprove_treat "Soil quality improved"
	la var vegetationreturned_treat "Vegetation returned"
	la var waterretention_treat "Water retained" 
	la var soilqualitydegrade_treat "Soil degraded"
	la var vegetationdisappeared_treat "Vegetation is gone"	

	coefplot (soilqualityimprove, label(Soil quality improved)) (vegetationreturned, label(Vegetation returned)) (waterretention, label(Water retained)) (soilqualitydegrade, label(Soil degraded)) (vegetationdisappeared, label(Vegetation is gone)) , keep (soilqualityimprove_treat vegetationreturned_treat waterretention_treat soilqualitydegrade_treat vegetationdisappeared_treat) xline(0) xtitle() legend(off) title() graphregion(margin(l=50)) yscale(alt noline) coeflabels(, labsize(medsmall) labgap(-125) notick ) mlcolor(black)  nooffsets

	graph export "$out/figa5.png", replace



************************************
*FIGURE A6:  SOIL QUALITY, OBSERVED*
************************************

	use "$data/dlconstruction2018-2020.dta", replace

	sum totalhectares_degraded if control==1
	summ totalhectares_degraded

	//There are some extreme outliers that represent less than .8 percent of the sample, thus this part of the distribution is dropped.  

	graph twoway (kdensity totalhectares_degraded if anytreat==0 & totalhectares_degraded<=7.5 & totalhectares_degraded>0)(kdensity totalhectares_degraded if anytreat==1 & totalhectares_degraded>0 & totalhectares_degraded<=7.5), ///
	ytitle("Density") xtitle("Total number of degraded hectares, Observed, Year 3") legend(label(1 "Control") label(2 "Treated"))
	graph export "$out/figa6.png", replace

	ksmirnov totalhectares_degraded if time==3 & totalhectares_degraded<=7.5 & totalhectares_degraded>0, by(anytreat)

*******************
*FIGURE A7: NUDGES*
*******************

use "$data/dlconstruction2021.dta", replace

	xi: reg interviewer_newdlcounthq nudge1 nudge2 nudge3 nudge4 nudge5 kantche vsize admin $tstat i.cluster, r cluster(villID)
	iegraph nudge1 nudge2 nudge3 nudge4 nudge5, basicti("") ///
	gray ytitle("New Demi-Lunes, Year 4") yscale(titlegap(5pt)) ///
	legend(off) xlabel(1 "No nudge" 2 "Permission" 3 "Timing" 4 "Ministry" 5 "Cost-benefit" 6 "Materials", ///
	labsize(medium) tstyle(major_notick)) ///
	graphregion(fcolor(white) lcolor(white)) xsize(6.5)
	graph export "$out/figa7.png", replace

clear

